<!DOCTYPE html><html lang="en" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>Pytorch框架深度学习图像分类模型 | 1025's Blog</title><meta name="author" content="1025"><meta name="copyright" content="1025"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="展示了图像分类数据集和图像识别经典模型，并给出了基于pytorch框架的模板。">
<meta property="og:type" content="article">
<meta property="og:title" content="Pytorch框架深度学习图像分类模型">
<meta property="og:url" content="https://lwkblog.gitee.io/2023/03/15/Pytorch%E6%A1%86%E6%9E%B6%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E5%9B%BE%E5%83%8F%E5%88%86%E7%B1%BB%E6%A8%A1%E5%9E%8B/index.html">
<meta property="og:site_name" content="1025&#39;s Blog">
<meta property="og:description" content="展示了图像分类数据集和图像识别经典模型，并给出了基于pytorch框架的模板。">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://pic1.zhimg.com/v2-5ea151d43c0ebb13546985d225ac256a_1200x500.jpg">
<meta property="article:published_time" content="2023-03-15T10:06:41.000Z">
<meta property="article:modified_time" content="2023-07-14T05:54:37.601Z">
<meta property="article:author" content="1025">
<meta property="article:tag" content="���ͣ�blog">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://pic1.zhimg.com/v2-5ea151d43c0ebb13546985d225ac256a_1200x500.jpg"><link rel="shortcut icon" href="https://www.logosc.cn/oss/icons/2022/05/27/nOPDe9rUUvMC63B.png"><link rel="canonical" href="https://lwkblog.gitee.io/2023/03/15/Pytorch%E6%A1%86%E6%9E%B6%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E5%9B%BE%E5%83%8F%E5%88%86%E7%B1%BB%E6%A8%A1%E5%9E%8B/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"We didn't find any results for the search: ${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: 'days',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: {"limitCount":50,"languages":{"author":"Author: 1025","link":"Link: ","source":"Source: 1025's Blog","info":"Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source."}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Pytorch框架深度学习图像分类模型',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-07-14 13:54:37'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = url => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      link.onload = () => resolve()
      link.onerror = () => reject()
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">Loading...</div></div></div><script>const preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',()=> { preloader.endLoading() })

if (true) {
  document.addEventListener('pjax:send', () => { preloader.initLoading() })
  document.addEventListener('pjax:complete', () => { preloader.endLoading() })
}</script><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://pic1.zhimg.com/50/v2-530bac941983a6aae866ab34b6290be2_720w.jpg?source=1940ef5c" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">12</div></a><a href="/tags/"><div class="headline">Tags</div><div class="length-num">0</div></a><a href="/categories/"><div class="headline">Categories</div><div class="length-num">5</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> List</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/gallery/"><i class="fa-fw fas fa-images"></i><span> Gallery</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> Music</span></a></li><li><a class="site-page child" href="/movie/"><i class="fa-fw fas fa-video"></i><span> Movie</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://pic1.zhimg.com/v2-5ea151d43c0ebb13546985d225ac256a_1200x500.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="1025's Blog"><img class="site-icon" src="/img/friend_404.gif"/><span class="site-name">1025's Blog</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> Search</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> List</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/gallery/"><i class="fa-fw fas fa-images"></i><span> Gallery</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> Music</span></a></li><li><a class="site-page child" href="/movie/"><i class="fa-fw fas fa-video"></i><span> Movie</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Pytorch框架深度学习图像分类模型</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">Created</span><time class="post-meta-date-created" datetime="2023-03-15T10:06:41.000Z" title="Created 2023-03-15 18:06:41">2023-03-15</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">Updated</span><time class="post-meta-date-updated" datetime="2023-07-14T05:54:37.601Z" title="Updated 2023-07-14 13:54:37">2023-07-14</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Deeplearn/">Deeplearn</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">Word count:</span><span class="word-count">6.6k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">Reading time:</span><span>39min</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Pytorch框架深度学习图像分类模型"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="首先声明！！！">首先声明！！！</h2>
<hr>
<ul>
<li>1.脚本为本人总结，如有使用注明出处。</li>
<li>2.Pytorch基于Python编程语言编写脚本。</li>
</ul>
<hr>
<h2 id="一、经典图像分类数据集：">一、经典图像分类数据集：</h2>
<h3 id="注：pytorch官网数据集-Datasets-—-Torchvision-0-15-documentation-pytorch-org">注：pytorch官网数据集 Datasets — Torchvision 0.15 documentation (<a target="_blank" rel="noopener" href="http://pytorch.org">pytorch.org</a>)</h3>
<h3 id="1-CIFAR10">1.CIFAR10</h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">transform = transforms.Compose([</span><br><span class="line">        transforms.Resize(<span class="number">256</span>),</span><br><span class="line">        transforms.CenterCrop(<span class="number">224</span>),</span><br><span class="line">        transforms.ToTensor(),</span><br><span class="line">        transforms.Normalize(mean=[<span class="number">0.485</span>, <span class="number">0.456</span>, <span class="number">0.406</span>], std=[<span class="number">0.229</span>, <span class="number">0.224</span>, <span class="number">0.225</span>]),</span><br><span class="line">    ])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 数据集路径</span></span><br><span class="line">    train_set = torchvision.datasets.CIFAR10(root=<span class="string">&quot;../data&quot;</span>, train=<span class="literal">True</span>, transform=transform)</span><br><span class="line">    test_set = torchvision.datasets.CIFAR10(root=<span class="string">&quot;../data&quot;</span>, train=<span class="literal">False</span>, transform=transform)</span><br><span class="line"></span><br><span class="line">    train_loader = torch.utils.data.DataLoader(train_set, batch_size=<span class="number">384</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">True</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line">    test_loader = torch.utils.data.DataLoader(test_set, batch_size=<span class="number">384</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">False</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>
<h3 id="2-CIFAR100">2.CIFAR100</h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">transform = transforms.Compose([</span><br><span class="line">        transforms.Resize(<span class="number">256</span>),</span><br><span class="line">        transforms.CenterCrop(<span class="number">224</span>),</span><br><span class="line">        transforms.ToTensor(),</span><br><span class="line">        transforms.Normalize(mean=[<span class="number">0.485</span>, <span class="number">0.456</span>, <span class="number">0.406</span>], std=[<span class="number">0.229</span>, <span class="number">0.224</span>, <span class="number">0.225</span>]),</span><br><span class="line">    ])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 数据集路径</span></span><br><span class="line">    train_set = torchvision.datasets.CIFAR100(root=<span class="string">&quot;../data&quot;</span>, train=<span class="literal">True</span>, transform=transform)</span><br><span class="line">    test_set = torchvision.datasets.CIFAR100(root=<span class="string">&quot;../data&quot;</span>, train=<span class="literal">False</span>, transform=transform)</span><br><span class="line"></span><br><span class="line">    train_loader = torch.utils.data.DataLoader(train_set, batch_size=<span class="number">128</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">True</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line">    test_loader = torch.utils.data.DataLoader(test_set, batch_size=<span class="number">128</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">False</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>
<h3 id="3-ImageNet-ILSVRC2012">3.ImageNet (ILSVRC2012)</h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">transform = transforms.Compose([</span><br><span class="line">        transforms.Resize(<span class="number">299</span>),</span><br><span class="line">        transforms.CenterCrop(<span class="number">299</span>),</span><br><span class="line">        transforms.ToTensor(),</span><br><span class="line">        transforms.Normalize(mean=[<span class="number">0.485</span>, <span class="number">0.456</span>, <span class="number">0.406</span>], std=[<span class="number">0.229</span>, <span class="number">0.224</span>, <span class="number">0.225</span>]),</span><br><span class="line">    ])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 数据集路径</span></span><br><span class="line">    train_set = torchvision.datasets.ImageFolder(root=<span class="string">&quot;/训练集的位置&quot;</span>, transform=transform)</span><br><span class="line">    test_set = torchvision.datasets.ImageFolder(root=<span class="string">&quot;/测试集的位置&quot;</span>, transform=transform)</span><br><span class="line"></span><br><span class="line">    train_loader = torch.utils.data.DataLoader(train_set, batch_size=<span class="number">32</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">True</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line">    test_loader = torch.utils.data.DataLoader(test_set, batch_size=<span class="number">32</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">False</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>
<h2 id="二、图像分类经典模型（基于CIFAR10数据集）：">二、图像分类经典模型（基于CIFAR10数据集）：</h2>
<h3 id="注：硬件配置显存最好有12g，根据显存大小调整batch-size的大小。">注：硬件配置显存最好有12g，根据显存大小调整batch_size的大小。</h3>
<h3 id="1-AlexNet（自己写的模型）">1.AlexNet（自己写的模型）</h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> torch.nn</span><br><span class="line"><span class="keyword">import</span> torchvision</span><br><span class="line"><span class="keyword">import</span> torch.optim <span class="keyword">as</span> optim</span><br><span class="line"><span class="keyword">import</span> torchvision.transforms <span class="keyword">as</span> transforms</span><br><span class="line"><span class="keyword">from</span> torch.utils.tensorboard <span class="keyword">import</span> SummaryWriter</span><br><span class="line"><span class="keyword">from</span> torch.utils.data <span class="keyword">import</span> DataLoader</span><br><span class="line"><span class="keyword">from</span> AlexNet <span class="keyword">import</span> *</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">weight_init</span>(<span class="params">m</span>):</span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">isinstance</span>(m, nn.Linear):</span><br><span class="line">        nn.init.xavier_normal_(m.weight)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">elif</span> <span class="built_in">isinstance</span>(m, nn.Conv2d):</span><br><span class="line">        nn.init.kaiming_normal_(m.weight, mode=<span class="string">&#x27;fan_out&#x27;</span>, nonlinearity=<span class="string">&#x27;relu&#x27;</span>)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">elif</span> <span class="built_in">isinstance</span>(m, nn.BatchNorm2d):</span><br><span class="line">        nn.init.constant_(m.weight, <span class="number">1</span>)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 主训练函数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">train</span>():</span><br><span class="line">    transform = transforms.Compose([</span><br><span class="line">        transforms.Resize(<span class="number">256</span>),</span><br><span class="line">        transforms.CenterCrop(<span class="number">224</span>),</span><br><span class="line">        transforms.ToTensor(),</span><br><span class="line">        transforms.Normalize(mean=[<span class="number">0.485</span>, <span class="number">0.456</span>, <span class="number">0.406</span>], std=[<span class="number">0.229</span>, <span class="number">0.224</span>, <span class="number">0.225</span>]),</span><br><span class="line">    ])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 数据集路径</span></span><br><span class="line">    train_set = torchvision.datasets.CIFAR10(root=<span class="string">&quot;../data&quot;</span>, train=<span class="literal">True</span>, transform=transform)</span><br><span class="line">    test_set = torchvision.datasets.CIFAR10(root=<span class="string">&quot;../data&quot;</span>, train=<span class="literal">False</span>, transform=transform)</span><br><span class="line"></span><br><span class="line">    train_loader = torch.utils.data.DataLoader(train_set, batch_size=<span class="number">384</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">True</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line">    test_loader = torch.utils.data.DataLoader(test_set, batch_size=<span class="number">384</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">False</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 写入tensorboard</span></span><br><span class="line">    writer = SummaryWriter(<span class="string">&quot;../train_AlexNet_1&quot;</span>)</span><br><span class="line"></span><br><span class="line">    train_data_size = <span class="built_in">len</span>(train_set)</span><br><span class="line">    test_data_size = <span class="built_in">len</span>(test_set)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;训练数据集的长度为：&#123;&#125;&quot;</span>.<span class="built_in">format</span>(train_data_size))</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;测试数据集的长度为：&#123;&#125; \n&quot;</span>.<span class="built_in">format</span>(test_data_size))</span><br><span class="line"></span><br><span class="line">    device = torch.device(<span class="string">&quot;cuda&quot;</span> <span class="keyword">if</span> torch.cuda.is_available() <span class="keyword">else</span> <span class="string">&quot;cpu&quot;</span>)</span><br><span class="line">    model = AlexNet()</span><br><span class="line">    model.to(device)</span><br><span class="line">    model.apply(weight_init)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 记录训练的轮数</span></span><br><span class="line">    total_train_step = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 记录测试的次数</span></span><br><span class="line">    total_test_step = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 控制训练轮数</span></span><br><span class="line">    epochs = <span class="number">60</span></span><br><span class="line"></span><br><span class="line">    optimizer = optim.SGD(model.parameters(), lr=<span class="number">1e-2</span>, momentum=<span class="number">0.9</span>, weight_decay=<span class="number">0.0005</span>)</span><br><span class="line">    criteon = nn.CrossEntropyLoss().to(device)</span><br><span class="line">    scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=<span class="number">100</span>, eta_min=<span class="number">0.0001</span>, last_epoch=-<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">    start_time = time.time()</span><br><span class="line">    <span class="keyword">for</span> epoch <span class="keyword">in</span> <span class="built_in">range</span>(epochs):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;-----第&#123;&#125;轮训练开始-----&quot;</span>.<span class="built_in">format</span>(epoch + <span class="number">1</span>))</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 训练开始</span></span><br><span class="line">        model.train()</span><br><span class="line">        <span class="keyword">global</span> train_loss</span><br><span class="line">        train_loss = <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> idx, (inputs, label) <span class="keyword">in</span> <span class="built_in">enumerate</span>(train_loader):</span><br><span class="line">            optimizer.zero_grad()</span><br><span class="line">            inputs, label = inputs.to(device), label.to(device)</span><br><span class="line">            outputs = model(inputs)</span><br><span class="line">            loss = criteon(outputs, label)</span><br><span class="line">            loss.backward()</span><br><span class="line">            optimizer.step()</span><br><span class="line">            train_loss += loss.item()</span><br><span class="line">            total_train_step += <span class="number">1</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;train_batchs:&#123;&#125;&quot;</span>.<span class="built_in">format</span>(total_train_step))</span><br><span class="line">        end_time = time.time()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;time: %.2fs&quot;</span> % (end_time - start_time))</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;train_loss:%.6f&quot;</span> % (train_loss / <span class="built_in">len</span>(train_set) * <span class="number">256</span>))</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 测试开始</span></span><br><span class="line">        model.<span class="built_in">eval</span>()</span><br><span class="line">        <span class="keyword">global</span> test_loss, correct</span><br><span class="line">        test_loss = <span class="number">0</span></span><br><span class="line">        correct = <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> idx, (inputs, label) <span class="keyword">in</span> <span class="built_in">enumerate</span>(test_loader):</span><br><span class="line">            <span class="keyword">with</span> torch.no_grad():</span><br><span class="line">                inputs, label = inputs.to(device), label.to(device)</span><br><span class="line">                outputs = model(inputs)</span><br><span class="line">                test_loss += criteon(outputs, label)</span><br><span class="line">                predict = torch.<span class="built_in">max</span>(outputs, dim=<span class="number">1</span>)[<span class="number">1</span>]</span><br><span class="line">                correct += torch.eq(predict, label).<span class="built_in">sum</span>().item()</span><br><span class="line">                total_test_step += <span class="number">1</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;test_batchs:&#123;&#125;&quot;</span>.<span class="built_in">format</span>(total_test_step))</span><br><span class="line">        end_time = time.time()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;time: %.2fs&quot;</span> % (end_time - start_time))</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;test_acc: %.6f  val_loss:%.6f \n&quot;</span> %</span><br><span class="line">              ((correct / <span class="built_in">len</span>(test_set)), test_loss * <span class="number">256</span> / <span class="built_in">len</span>(test_set)))</span><br><span class="line"></span><br><span class="line">        writer.add_scalar(<span class="string">&#x27;loss&#x27;</span>, train_loss / <span class="built_in">len</span>(train_loader), epoch)</span><br><span class="line">        writer.add_scalar(<span class="string">&#x27;acc&#x27;</span>, correct / <span class="built_in">len</span>(test_set), epoch)</span><br><span class="line">        scheduler.step()</span><br><span class="line"></span><br><span class="line">    torch.save(model, <span class="string">&#x27;../models/AlexNet_1.pth&#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;---Finished Training---&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    writer.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    train()</span><br><span class="line">    </span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> torch.nn <span class="keyword">as</span> nn</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 手搓一个AlexNet模型</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">AlexNet</span>(nn.Module):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="built_in">super</span>(AlexNet, self).__init__()</span><br><span class="line"></span><br><span class="line">        self.conv1 = nn.Sequential(</span><br><span class="line">                                   nn.Conv2d(<span class="number">3</span>, <span class="number">96</span>, <span class="number">11</span>, <span class="number">4</span>, <span class="number">2</span>),</span><br><span class="line">                                   nn.ReLU(),</span><br><span class="line">                                   nn.MaxPool2d(<span class="number">3</span>, <span class="number">2</span>),</span><br><span class="line">                                  )</span><br><span class="line"></span><br><span class="line">        self.conv2 = nn.Sequential(</span><br><span class="line">                                   nn.Conv2d(<span class="number">96</span>, <span class="number">256</span>, <span class="number">5</span>, <span class="number">1</span>, <span class="number">2</span>),</span><br><span class="line">                                   nn.ReLU(),</span><br><span class="line">                                   nn.MaxPool2d(<span class="number">3</span>, <span class="number">2</span>),</span><br><span class="line">                                  )</span><br><span class="line"></span><br><span class="line">        self.conv3 = nn.Sequential(</span><br><span class="line">                                   nn.Conv2d(<span class="number">256</span>, <span class="number">384</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">1</span>),</span><br><span class="line">                                   nn.ReLU(),</span><br><span class="line">                                   nn.Conv2d(<span class="number">384</span>, <span class="number">384</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">1</span>),</span><br><span class="line">                                   nn.ReLU(),</span><br><span class="line">                                   nn.Conv2d(<span class="number">384</span>, <span class="number">256</span>, <span class="number">3</span>, <span class="number">1</span>, <span class="number">1</span>),</span><br><span class="line">                                   nn.ReLU(),</span><br><span class="line">                                   nn.MaxPool2d(<span class="number">3</span>, <span class="number">2</span>)</span><br><span class="line">                                  )</span><br><span class="line"></span><br><span class="line">        self.fc = nn.Sequential(</span><br><span class="line">                                nn.Linear(<span class="number">256</span> * <span class="number">6</span> * <span class="number">6</span>, <span class="number">4096</span>),</span><br><span class="line">                                nn.ReLU(),</span><br><span class="line">                                nn.Dropout(<span class="number">0.5</span>),</span><br><span class="line">                                nn.Linear(<span class="number">4096</span>, <span class="number">4096</span>),</span><br><span class="line">                                nn.ReLU(),</span><br><span class="line">                                nn.Dropout(<span class="number">0.5</span>),</span><br><span class="line">                                nn.Linear(<span class="number">4096</span>, <span class="number">100</span>),</span><br><span class="line">                               )</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">forward</span>(<span class="params">self, x</span>):</span><br><span class="line">        x = self.conv1(x)</span><br><span class="line">        x = self.conv2(x)</span><br><span class="line">        x = self.conv3(x)</span><br><span class="line">        output = self.fc(x.view(-<span class="number">1</span>, <span class="number">256</span> * <span class="number">6</span> * <span class="number">6</span>))</span><br><span class="line">        <span class="keyword">return</span> output</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="2-VGG（models-vgg16-）">2.VGG（models.vgg16()）</h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> torch.nn</span><br><span class="line"><span class="keyword">import</span> torchvision</span><br><span class="line"><span class="keyword">import</span> torch.optim <span class="keyword">as</span> optim</span><br><span class="line"><span class="keyword">import</span> torchvision.transforms <span class="keyword">as</span> transforms</span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> nn</span><br><span class="line"><span class="keyword">from</span> torch.utils.tensorboard <span class="keyword">import</span> SummaryWriter</span><br><span class="line"><span class="keyword">from</span> torch.utils.data <span class="keyword">import</span> DataLoader</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">weight_init</span>(<span class="params">m</span>):</span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">isinstance</span>(m, nn.Linear):</span><br><span class="line">        nn.init.xavier_normal_(m.weight)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">elif</span> <span class="built_in">isinstance</span>(m, nn.Conv2d):</span><br><span class="line">        nn.init.kaiming_normal_(m.weight, mode=<span class="string">&#x27;fan_out&#x27;</span>, nonlinearity=<span class="string">&#x27;relu&#x27;</span>)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">elif</span> <span class="built_in">isinstance</span>(m, nn.BatchNorm2d):</span><br><span class="line">        nn.init.constant_(m.weight, <span class="number">1</span>)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 主训练函数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">train</span>():</span><br><span class="line">    transform = transforms.Compose(</span><br><span class="line">        [transforms.ToTensor(),</span><br><span class="line">         transforms.Resize((<span class="number">224</span>, <span class="number">224</span>)),</span><br><span class="line">         transforms.Normalize([<span class="number">0.485</span>, <span class="number">0.456</span>, <span class="number">0.406</span>], [<span class="number">0.229</span>, <span class="number">0.224</span>, <span class="number">0.225</span>])])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 数据集路径</span></span><br><span class="line">    train_set = torchvision.datasets.CIFAR10(root=<span class="string">&quot;../data&quot;</span>, train=<span class="literal">True</span>, transform=transform)</span><br><span class="line">    test_set = torchvision.datasets.CIFAR10(root=<span class="string">&quot;../data&quot;</span>, train=<span class="literal">False</span>, transform=transform)</span><br><span class="line"></span><br><span class="line">    train_loader = torch.utils.data.DataLoader(train_set, batch_size=<span class="number">64</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">True</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line">    test_loader = torch.utils.data.DataLoader(test_set, batch_size=<span class="number">64</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">False</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 写入tensorboard</span></span><br><span class="line">    writer = SummaryWriter(<span class="string">&quot;../train_VGG_1&quot;</span>)</span><br><span class="line"></span><br><span class="line">    train_data_size = <span class="built_in">len</span>(train_set)</span><br><span class="line">    test_data_size = <span class="built_in">len</span>(test_set)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;训练数据集的长度为：&#123;&#125;&quot;</span>.<span class="built_in">format</span>(train_data_size))</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;测试数据集的长度为：&#123;&#125; \n&quot;</span>.<span class="built_in">format</span>(test_data_size))</span><br><span class="line"></span><br><span class="line">    device = torch.device(<span class="string">&quot;cuda&quot;</span> <span class="keyword">if</span> torch.cuda.is_available() <span class="keyword">else</span> <span class="string">&quot;cpu&quot;</span>)</span><br><span class="line">    model = torchvision.models.vgg16()</span><br><span class="line">    model.to(device)</span><br><span class="line">    <span class="comment"># model.apply(weight_init)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 记录训练的轮数</span></span><br><span class="line">    total_train_step = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 记录测试的次数</span></span><br><span class="line">    total_test_step = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 控制训练轮数</span></span><br><span class="line">    epochs = <span class="number">30</span></span><br><span class="line"></span><br><span class="line">    optimizer = optim.SGD(model.parameters(), lr=<span class="number">1e-2</span>, momentum=<span class="number">0.9</span>, weight_decay=<span class="number">0.0005</span>)</span><br><span class="line">    criteon = nn.CrossEntropyLoss().to(device)</span><br><span class="line">    scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=<span class="number">100</span>, eta_min=<span class="number">0.0001</span>, last_epoch=-<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">    start_time = time.time()</span><br><span class="line">    <span class="keyword">for</span> epoch <span class="keyword">in</span> <span class="built_in">range</span>(epochs):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;-----第&#123;&#125;轮训练开始-----&quot;</span>.<span class="built_in">format</span>(epoch + <span class="number">1</span>))</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 训练开始</span></span><br><span class="line">        model.train()</span><br><span class="line">        <span class="keyword">global</span> train_loss</span><br><span class="line">        train_loss = <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> idx, (inputs, label) <span class="keyword">in</span> <span class="built_in">enumerate</span>(train_loader):</span><br><span class="line">            optimizer.zero_grad()</span><br><span class="line">            inputs, label = inputs.to(device), label.to(device)</span><br><span class="line">            outputs = model(inputs)</span><br><span class="line">            loss = criteon(outputs, label)</span><br><span class="line">            loss.backward()</span><br><span class="line">            optimizer.step()</span><br><span class="line">            train_loss += loss.item()</span><br><span class="line">            total_train_step += <span class="number">1</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;train_batchs:&#123;&#125;&quot;</span>.<span class="built_in">format</span>(total_train_step))</span><br><span class="line">        end_time = time.time()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;time: %.2fs&quot;</span> % (end_time - start_time))</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;train_loss:%.6f&quot;</span> % (train_loss / <span class="built_in">len</span>(train_set) * <span class="number">256</span>))</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 测试开始</span></span><br><span class="line">        model.<span class="built_in">eval</span>()</span><br><span class="line">        <span class="keyword">global</span> test_loss, correct</span><br><span class="line">        test_loss = <span class="number">0</span></span><br><span class="line">        correct = <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> idx, (inputs, label) <span class="keyword">in</span> <span class="built_in">enumerate</span>(test_loader):</span><br><span class="line">            <span class="keyword">with</span> torch.no_grad():</span><br><span class="line">                inputs, label = inputs.to(device), label.to(device)</span><br><span class="line">                outputs = model(inputs)</span><br><span class="line">                test_loss += criteon(outputs, label)</span><br><span class="line">                predict = torch.<span class="built_in">max</span>(outputs, dim=<span class="number">1</span>)[<span class="number">1</span>]</span><br><span class="line">                correct += torch.eq(predict, label).<span class="built_in">sum</span>().item()</span><br><span class="line">                total_test_step += <span class="number">1</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;test_batchs:&#123;&#125;&quot;</span>.<span class="built_in">format</span>(total_test_step))</span><br><span class="line">        end_time = time.time()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;time: %.2fs&quot;</span> % (end_time - start_time))</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;test_acc: %.6f  val_loss:%.6f \n&quot;</span> %</span><br><span class="line">              ((correct / <span class="built_in">len</span>(test_set)), test_loss * <span class="number">256</span> / <span class="built_in">len</span>(test_set)))</span><br><span class="line"></span><br><span class="line">        writer.add_scalar(<span class="string">&#x27;loss&#x27;</span>, train_loss / <span class="built_in">len</span>(train_loader), epoch)</span><br><span class="line">        writer.add_scalar(<span class="string">&#x27;acc&#x27;</span>, correct / <span class="built_in">len</span>(test_set), epoch)</span><br><span class="line">        scheduler.step()</span><br><span class="line"></span><br><span class="line">    torch.save(model, <span class="string">&#x27;../models/VGG_1.pth&#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;---Finished Training---&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    writer.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    train()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="3-GoogLeNet">3.GoogLeNet</h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> torch.nn</span><br><span class="line"><span class="keyword">import</span> torchvision</span><br><span class="line"><span class="keyword">import</span> torch.optim <span class="keyword">as</span> optim</span><br><span class="line"><span class="keyword">import</span> torchvision.transforms <span class="keyword">as</span> transforms</span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> nn</span><br><span class="line"><span class="keyword">from</span> torch.utils.tensorboard <span class="keyword">import</span> SummaryWriter</span><br><span class="line"><span class="keyword">from</span> torch.utils.data <span class="keyword">import</span> DataLoader</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">weight_init</span>(<span class="params">m</span>):</span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">isinstance</span>(m, nn.Linear):</span><br><span class="line">        nn.init.xavier_normal_(m.weight)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">elif</span> <span class="built_in">isinstance</span>(m, nn.Conv2d):</span><br><span class="line">        nn.init.kaiming_normal_(m.weight, mode=<span class="string">&#x27;fan_out&#x27;</span>, nonlinearity=<span class="string">&#x27;relu&#x27;</span>)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">elif</span> <span class="built_in">isinstance</span>(m, nn.BatchNorm2d):</span><br><span class="line">        nn.init.constant_(m.weight, <span class="number">1</span>)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 主训练函数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">train</span>():</span><br><span class="line">    transform = transforms.Compose([</span><br><span class="line">        transforms.Resize(<span class="number">256</span>),</span><br><span class="line">        transforms.CenterCrop(<span class="number">224</span>),</span><br><span class="line">        transforms.ToTensor(),</span><br><span class="line">        transforms.Normalize(mean=[<span class="number">0.485</span>, <span class="number">0.456</span>, <span class="number">0.406</span>], std=[<span class="number">0.229</span>, <span class="number">0.224</span>, <span class="number">0.225</span>]),</span><br><span class="line">    ])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 数据集路径</span></span><br><span class="line">    train_set = torchvision.datasets.CIFAR10(root=<span class="string">&quot;../data&quot;</span>, train=<span class="literal">True</span>, transform=transform)</span><br><span class="line">    test_set = torchvision.datasets.CIFAR10(root=<span class="string">&quot;../data&quot;</span>, train=<span class="literal">False</span>, transform=transform)</span><br><span class="line"></span><br><span class="line">    train_loader = torch.utils.data.DataLoader(train_set, batch_size=<span class="number">128</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">True</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line">    test_loader = torch.utils.data.DataLoader(test_set, batch_size=<span class="number">128</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">False</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 写入tensorboard</span></span><br><span class="line">    writer = SummaryWriter(<span class="string">&quot;../train_GoogLeNet_1&quot;</span>)</span><br><span class="line"></span><br><span class="line">    train_data_size = <span class="built_in">len</span>(train_set)</span><br><span class="line">    test_data_size = <span class="built_in">len</span>(test_set)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;训练数据集的长度为：&#123;&#125;&quot;</span>.<span class="built_in">format</span>(train_data_size))</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;测试数据集的长度为：&#123;&#125; \n&quot;</span>.<span class="built_in">format</span>(test_data_size))</span><br><span class="line"></span><br><span class="line">    device = torch.device(<span class="string">&quot;cuda&quot;</span> <span class="keyword">if</span> torch.cuda.is_available() <span class="keyword">else</span> <span class="string">&quot;cpu&quot;</span>)</span><br><span class="line">    model = torchvision.models.GoogLeNet()</span><br><span class="line">    model.to(device)</span><br><span class="line">    <span class="comment"># model.apply(weight_init)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 记录训练的轮数</span></span><br><span class="line">    total_train_step = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 记录测试的次数</span></span><br><span class="line">    total_test_step = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 控制训练轮数</span></span><br><span class="line">    epochs = <span class="number">50</span></span><br><span class="line"></span><br><span class="line">    optimizer = optim.SGD(model.parameters(), lr=<span class="number">1e-2</span>, momentum=<span class="number">0.9</span>, weight_decay=<span class="number">0.0005</span>)</span><br><span class="line">    criteon = nn.CrossEntropyLoss().to(device)</span><br><span class="line">    scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=<span class="number">100</span>, eta_min=<span class="number">0.0001</span>, last_epoch=-<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">    start_time = time.time()</span><br><span class="line">    <span class="keyword">for</span> epoch <span class="keyword">in</span> <span class="built_in">range</span>(epochs):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;-----第&#123;&#125;轮训练开始-----&quot;</span>.<span class="built_in">format</span>(epoch + <span class="number">1</span>))</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 训练开始</span></span><br><span class="line">        model.train()</span><br><span class="line">        <span class="keyword">global</span> train_loss</span><br><span class="line">        train_loss = <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> idx, (inputs, label) <span class="keyword">in</span> <span class="built_in">enumerate</span>(train_loader):</span><br><span class="line">            optimizer.zero_grad()</span><br><span class="line">            inputs, label = inputs.to(device), label.to(device)</span><br><span class="line">            outputs = model(inputs)</span><br><span class="line">            outputs = outputs.logits</span><br><span class="line">            loss = criteon(outputs, label)</span><br><span class="line">            loss.backward()</span><br><span class="line">            optimizer.step()</span><br><span class="line">            train_loss += loss.item()</span><br><span class="line">            total_train_step += <span class="number">1</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;train_batchs:&#123;&#125;&quot;</span>.<span class="built_in">format</span>(total_train_step))</span><br><span class="line">        end_time = time.time()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;time: %.1fs&quot;</span> % (end_time - start_time))</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;train_loss:%.5f&quot;</span> % (train_loss / <span class="built_in">len</span>(train_set) * <span class="number">256</span>))</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 测试开始</span></span><br><span class="line">        model.<span class="built_in">eval</span>()</span><br><span class="line">        <span class="keyword">global</span> test_loss, correct</span><br><span class="line">        test_loss = <span class="number">0</span></span><br><span class="line">        correct = <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> idx, (inputs, label) <span class="keyword">in</span> <span class="built_in">enumerate</span>(test_loader):</span><br><span class="line">            <span class="keyword">with</span> torch.no_grad():</span><br><span class="line">                inputs, label = inputs.to(device), label.to(device)</span><br><span class="line">                outputs = model(inputs)</span><br><span class="line">                test_loss += criteon(outputs, label)</span><br><span class="line">                predict = torch.<span class="built_in">max</span>(outputs, dim=<span class="number">1</span>)[<span class="number">1</span>]</span><br><span class="line">                correct += torch.eq(predict, label).<span class="built_in">sum</span>().item()</span><br><span class="line">                total_test_step += <span class="number">1</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;test_batchs:&#123;&#125;&quot;</span>.<span class="built_in">format</span>(total_test_step))</span><br><span class="line">        end_time = time.time()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;time: %.1fs&quot;</span> % (end_time - start_time))</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;test_acc: %.5f  val_loss:%.5f \n&quot;</span> %</span><br><span class="line">              ((correct / <span class="built_in">len</span>(test_set)), test_loss * <span class="number">256</span> / <span class="built_in">len</span>(test_set)))</span><br><span class="line"></span><br><span class="line">        writer.add_scalar(<span class="string">&#x27;loss&#x27;</span>, train_loss / <span class="built_in">len</span>(train_loader), epoch)</span><br><span class="line">        writer.add_scalar(<span class="string">&#x27;acc&#x27;</span>, correct / <span class="built_in">len</span>(test_set), epoch)</span><br><span class="line">        scheduler.step()</span><br><span class="line"></span><br><span class="line">    torch.save(model, <span class="string">&#x27;../models/GooLeNet_1.pth&#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;---Finished Training---&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    writer.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    train()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="4-ResNet（models-resnet101-）">4.ResNet（models.resnet101()）</h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> torch.nn</span><br><span class="line"><span class="keyword">import</span> torchvision</span><br><span class="line"><span class="keyword">import</span> torch.optim <span class="keyword">as</span> optim</span><br><span class="line"><span class="keyword">import</span> torchvision.transforms <span class="keyword">as</span> transforms</span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> nn</span><br><span class="line"><span class="keyword">from</span> torch.utils.tensorboard <span class="keyword">import</span> SummaryWriter</span><br><span class="line"><span class="keyword">from</span> torch.utils.data <span class="keyword">import</span> DataLoader</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">weight_init</span>(<span class="params">m</span>):</span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">isinstance</span>(m, nn.Linear):</span><br><span class="line">        nn.init.xavier_normal_(m.weight)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">elif</span> <span class="built_in">isinstance</span>(m, nn.Conv2d):</span><br><span class="line">        nn.init.kaiming_normal_(m.weight, mode=<span class="string">&#x27;fan_out&#x27;</span>, nonlinearity=<span class="string">&#x27;relu&#x27;</span>)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">elif</span> <span class="built_in">isinstance</span>(m, nn.BatchNorm2d):</span><br><span class="line">        nn.init.constant_(m.weight, <span class="number">1</span>)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 主训练函数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">train</span>():</span><br><span class="line">    transform = transforms.Compose(</span><br><span class="line">        [transforms.ToTensor(),</span><br><span class="line">         transforms.Resize((<span class="number">224</span>, <span class="number">224</span>)),</span><br><span class="line">         transforms.Normalize([<span class="number">0.485</span>, <span class="number">0.456</span>, <span class="number">0.406</span>], [<span class="number">0.229</span>, <span class="number">0.224</span>, <span class="number">0.225</span>])])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 数据集路径</span></span><br><span class="line">    train_set = torchvision.datasets.CIFAR10(root=<span class="string">&quot;../data&quot;</span>, train=<span class="literal">True</span>, transform=transform)</span><br><span class="line">    test_set = torchvision.datasets.CIFAR10(root=<span class="string">&quot;../data&quot;</span>, train=<span class="literal">False</span>, transform=transform)</span><br><span class="line"></span><br><span class="line">    train_loader = torch.utils.data.DataLoader(train_set, batch_size=<span class="number">64</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">True</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line">    test_loader = torch.utils.data.DataLoader(test_set, batch_size=<span class="number">64</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">False</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 写入tensorboard</span></span><br><span class="line">    writer = SummaryWriter(<span class="string">&quot;../train_ResNet_1&quot;</span>)</span><br><span class="line"></span><br><span class="line">    train_data_size = <span class="built_in">len</span>(train_set)</span><br><span class="line">    test_data_size = <span class="built_in">len</span>(test_set)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;训练数据集的长度为：&#123;&#125;&quot;</span>.<span class="built_in">format</span>(train_data_size))</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;测试数据集的长度为：&#123;&#125; \n&quot;</span>.<span class="built_in">format</span>(test_data_size))</span><br><span class="line"></span><br><span class="line">    device = torch.device(<span class="string">&quot;cuda&quot;</span> <span class="keyword">if</span> torch.cuda.is_available() <span class="keyword">else</span> <span class="string">&quot;cpu&quot;</span>)</span><br><span class="line">    model = torchvision.models.resnet101()</span><br><span class="line">    model.to(device)</span><br><span class="line">    <span class="comment"># model.apply(weight_init)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 记录训练的轮数</span></span><br><span class="line">    total_train_step = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 记录测试的次数</span></span><br><span class="line">    total_test_step = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 控制训练轮数</span></span><br><span class="line">    epochs = <span class="number">30</span></span><br><span class="line"></span><br><span class="line">    optimizer = optim.SGD(model.parameters(), lr=<span class="number">1e-2</span>, momentum=<span class="number">0.9</span>, weight_decay=<span class="number">0.0005</span>)</span><br><span class="line">    criteon = nn.CrossEntropyLoss().to(device)</span><br><span class="line">    scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=<span class="number">100</span>, eta_min=<span class="number">0.0001</span>, last_epoch=-<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">    start_time = time.time()</span><br><span class="line">    <span class="keyword">for</span> epoch <span class="keyword">in</span> <span class="built_in">range</span>(epochs):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;-----第&#123;&#125;轮训练开始-----&quot;</span>.<span class="built_in">format</span>(epoch + <span class="number">1</span>))</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 训练开始</span></span><br><span class="line">        model.train()</span><br><span class="line">        <span class="keyword">global</span> train_loss</span><br><span class="line">        train_loss = <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> idx, (inputs, label) <span class="keyword">in</span> <span class="built_in">enumerate</span>(train_loader):</span><br><span class="line">            optimizer.zero_grad()</span><br><span class="line">            inputs, label = inputs.to(device), label.to(device)</span><br><span class="line">            outputs = model(inputs)</span><br><span class="line">            loss = criteon(outputs, label)</span><br><span class="line">            loss.backward()</span><br><span class="line">            optimizer.step()</span><br><span class="line">            train_loss += loss.item()</span><br><span class="line">            total_train_step += <span class="number">1</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;train_batchs:&#123;&#125;&quot;</span>.<span class="built_in">format</span>(total_train_step))</span><br><span class="line">        end_time = time.time()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;time: %.2fs&quot;</span> % (end_time - start_time))</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;train_loss:%.6f&quot;</span> % (train_loss / <span class="built_in">len</span>(train_set) * <span class="number">256</span>))</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 测试开始</span></span><br><span class="line">        model.<span class="built_in">eval</span>()</span><br><span class="line">        <span class="keyword">global</span> test_loss, correct</span><br><span class="line">        test_loss = <span class="number">0</span></span><br><span class="line">        correct = <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> idx, (inputs, label) <span class="keyword">in</span> <span class="built_in">enumerate</span>(test_loader):</span><br><span class="line">            <span class="keyword">with</span> torch.no_grad():</span><br><span class="line">                inputs, label = inputs.to(device), label.to(device)</span><br><span class="line">                outputs = model(inputs)</span><br><span class="line">                test_loss += criteon(outputs, label)</span><br><span class="line">                predict = torch.<span class="built_in">max</span>(outputs, dim=<span class="number">1</span>)[<span class="number">1</span>]</span><br><span class="line">                correct += torch.eq(predict, label).<span class="built_in">sum</span>().item()</span><br><span class="line">                total_test_step += <span class="number">1</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;test_batchs:&#123;&#125;&quot;</span>.<span class="built_in">format</span>(total_test_step))</span><br><span class="line">        end_time = time.time()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;time: %.2fs&quot;</span> % (end_time - start_time))</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;test_acc: %.6f  val_loss:%.6f \n&quot;</span> %</span><br><span class="line">              ((correct / <span class="built_in">len</span>(test_set)), test_loss * <span class="number">256</span> / <span class="built_in">len</span>(test_set)))</span><br><span class="line"></span><br><span class="line">        writer.add_scalar(<span class="string">&#x27;loss&#x27;</span>, train_loss / <span class="built_in">len</span>(train_loader), epoch)</span><br><span class="line">        writer.add_scalar(<span class="string">&#x27;acc&#x27;</span>, correct / <span class="built_in">len</span>(test_set), epoch)</span><br><span class="line">        scheduler.step()</span><br><span class="line"></span><br><span class="line">    torch.save(model, <span class="string">&#x27;../models/ResNet_1.pth&#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;---Finished Training---&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    writer.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    train()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="5-DenseNet（models-densenet121-）">5.DenseNet（models.densenet121()）</h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> torch.nn</span><br><span class="line"><span class="keyword">import</span> torchvision</span><br><span class="line"><span class="keyword">import</span> torch.optim <span class="keyword">as</span> optim</span><br><span class="line"><span class="keyword">import</span> torchvision.transforms <span class="keyword">as</span> transforms</span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> nn</span><br><span class="line"><span class="keyword">from</span> torch.utils.tensorboard <span class="keyword">import</span> SummaryWriter</span><br><span class="line"><span class="keyword">from</span> torch.utils.data <span class="keyword">import</span> DataLoader</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">weight_init</span>(<span class="params">m</span>):</span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">isinstance</span>(m, nn.Linear):</span><br><span class="line">        nn.init.xavier_normal_(m.weight)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">elif</span> <span class="built_in">isinstance</span>(m, nn.Conv2d):</span><br><span class="line">        nn.init.kaiming_normal_(m.weight, mode=<span class="string">&#x27;fan_out&#x27;</span>, nonlinearity=<span class="string">&#x27;relu&#x27;</span>)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">elif</span> <span class="built_in">isinstance</span>(m, nn.BatchNorm2d):</span><br><span class="line">        nn.init.constant_(m.weight, <span class="number">1</span>)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 主训练函数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">train</span>():</span><br><span class="line">    transform = transforms.Compose(</span><br><span class="line">        [transforms.ToTensor(),</span><br><span class="line">         transforms.Resize((<span class="number">224</span>, <span class="number">224</span>)),</span><br><span class="line">         transforms.Normalize([<span class="number">0.485</span>, <span class="number">0.456</span>, <span class="number">0.406</span>], [<span class="number">0.229</span>, <span class="number">0.224</span>, <span class="number">0.225</span>])])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 数据集路径</span></span><br><span class="line">    train_set = torchvision.datasets.CIFAR10(root=<span class="string">&quot;../data&quot;</span>, transform=transform, train=<span class="literal">True</span>)</span><br><span class="line">    test_set = torchvision.datasets.CIFAR10(root=<span class="string">&quot;../data&quot;</span>, transform=transform, train=<span class="literal">False</span>)</span><br><span class="line"></span><br><span class="line">    train_loader = torch.utils.data.DataLoader(train_set, batch_size=<span class="number">64</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">True</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line">    test_loader = torch.utils.data.DataLoader(test_set, batch_size=<span class="number">64</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">False</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 写入tensorboard</span></span><br><span class="line">    writer = SummaryWriter(<span class="string">&quot;../train_DenseNet_1&quot;</span>)</span><br><span class="line"></span><br><span class="line">    train_data_size = <span class="built_in">len</span>(train_set)</span><br><span class="line">    test_data_size = <span class="built_in">len</span>(test_set)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;训练数据集的长度为：&#123;&#125;&quot;</span>.<span class="built_in">format</span>(train_data_size))</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;测试数据集的长度为：&#123;&#125; \n&quot;</span>.<span class="built_in">format</span>(test_data_size))</span><br><span class="line"></span><br><span class="line">    device = torch.device(<span class="string">&quot;cuda&quot;</span> <span class="keyword">if</span> torch.cuda.is_available() <span class="keyword">else</span> <span class="string">&quot;cpu&quot;</span>)</span><br><span class="line">    model = torchvision.models.densenet121()</span><br><span class="line">    model.to(device)</span><br><span class="line">    <span class="comment"># model.apply(weight_init)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 记录训练的轮数</span></span><br><span class="line">    total_train_step = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 记录测试的次数</span></span><br><span class="line">    total_test_step = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 控制训练轮数</span></span><br><span class="line">    epochs = <span class="number">30</span></span><br><span class="line"></span><br><span class="line">    optimizer = optim.SGD(model.parameters(), lr=<span class="number">1e-2</span>, momentum=<span class="number">0.9</span>, weight_decay=<span class="number">0.0005</span>)</span><br><span class="line">    criteon = nn.CrossEntropyLoss().to(device)</span><br><span class="line">    scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=<span class="number">100</span>, eta_min=<span class="number">0.0001</span>, last_epoch=-<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">    start_time = time.time()</span><br><span class="line">    <span class="keyword">for</span> epoch <span class="keyword">in</span> <span class="built_in">range</span>(epochs):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;-----第&#123;&#125;轮训练开始-----&quot;</span>.<span class="built_in">format</span>(epoch + <span class="number">1</span>))</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 训练开始</span></span><br><span class="line">        model.train()</span><br><span class="line">        <span class="keyword">global</span> train_loss</span><br><span class="line">        train_loss = <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> idx, (inputs, label) <span class="keyword">in</span> <span class="built_in">enumerate</span>(train_loader):</span><br><span class="line">            optimizer.zero_grad()</span><br><span class="line">            inputs, label = inputs.to(device), label.to(device)</span><br><span class="line">            outputs = model(inputs)</span><br><span class="line">            loss = criteon(outputs, label)</span><br><span class="line">            loss.backward()</span><br><span class="line">            optimizer.step()</span><br><span class="line">            train_loss += loss.item()</span><br><span class="line">            total_train_step += <span class="number">1</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;train_batchs:&#123;&#125;&quot;</span>.<span class="built_in">format</span>(total_train_step))</span><br><span class="line">        end_time = time.time()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;time: %.2fs&quot;</span> % (end_time - start_time))</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;train_loss:%.6f&quot;</span> % (train_loss / <span class="built_in">len</span>(train_set) * <span class="number">256</span>))</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 测试开始  dgsd</span></span><br><span class="line">        model.<span class="built_in">eval</span>()</span><br><span class="line">        <span class="keyword">global</span> test_loss, correct</span><br><span class="line">        test_loss = <span class="number">0</span></span><br><span class="line">        correct = <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> idx, (inputs, label) <span class="keyword">in</span> <span class="built_in">enumerate</span>(test_loader):</span><br><span class="line">            <span class="keyword">with</span> torch.no_grad():</span><br><span class="line">                inputs, label = inputs.to(device), label.to(device)</span><br><span class="line">                outputs = model(inputs)</span><br><span class="line">                test_loss += criteon(outputs, label)</span><br><span class="line">                predict = torch.<span class="built_in">max</span>(outputs, dim=<span class="number">1</span>)[<span class="number">1</span>]</span><br><span class="line">                correct += torch.eq(predict, label).<span class="built_in">sum</span>().item()</span><br><span class="line">                total_test_step += <span class="number">1</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;test_batchs:&#123;&#125;&quot;</span>.<span class="built_in">format</span>(total_test_step))</span><br><span class="line">        end_time = time.time()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;time: %.2fs&quot;</span> % (end_time - start_time))</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;test_acc: %.6f  val_loss:%.6f \n&quot;</span> %</span><br><span class="line">              ((correct / <span class="built_in">len</span>(test_set)), test_loss * <span class="number">256</span> / <span class="built_in">len</span>(test_set)))</span><br><span class="line"></span><br><span class="line">        writer.add_scalar(<span class="string">&#x27;loss&#x27;</span>, train_loss / <span class="built_in">len</span>(train_loader), epoch)</span><br><span class="line">        writer.add_scalar(<span class="string">&#x27;acc&#x27;</span>, correct / <span class="built_in">len</span>(test_set), epoch)</span><br><span class="line">        scheduler.step()</span><br><span class="line"></span><br><span class="line">    torch.save(model, <span class="string">&#x27;../models/DenseNet_1.pth&#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;---Finished Training---&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    writer.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    train()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="6-Train-ImageNet（main-py）">6.Train_ImageNet（<a target="_blank" rel="noopener" href="http://main.py">main.py</a>）</h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br><span class="line">425</span><br><span class="line">426</span><br><span class="line">427</span><br><span class="line">428</span><br><span class="line">429</span><br><span class="line">430</span><br><span class="line">431</span><br><span class="line">432</span><br><span class="line">433</span><br><span class="line">434</span><br><span class="line">435</span><br><span class="line">436</span><br><span class="line">437</span><br><span class="line">438</span><br><span class="line">439</span><br><span class="line">440</span><br><span class="line">441</span><br><span class="line">442</span><br><span class="line">443</span><br><span class="line">444</span><br><span class="line">445</span><br><span class="line">446</span><br><span class="line">447</span><br><span class="line">448</span><br><span class="line">449</span><br><span class="line">450</span><br><span class="line">451</span><br><span class="line">452</span><br><span class="line">453</span><br><span class="line">454</span><br><span class="line">455</span><br><span class="line">456</span><br><span class="line">457</span><br><span class="line">458</span><br><span class="line">459</span><br><span class="line">460</span><br><span class="line">461</span><br><span class="line">462</span><br><span class="line">463</span><br><span class="line">464</span><br><span class="line">465</span><br><span class="line">466</span><br><span class="line">467</span><br><span class="line">468</span><br><span class="line">469</span><br><span class="line">470</span><br><span class="line">471</span><br><span class="line">472</span><br><span class="line">473</span><br><span class="line">474</span><br><span class="line">475</span><br><span class="line">476</span><br><span class="line">477</span><br><span class="line">478</span><br><span class="line">479</span><br><span class="line">480</span><br><span class="line">481</span><br><span class="line">482</span><br><span class="line">483</span><br><span class="line">484</span><br><span class="line">485</span><br><span class="line">486</span><br><span class="line">487</span><br><span class="line">488</span><br><span class="line">489</span><br><span class="line">490</span><br><span class="line">491</span><br><span class="line">492</span><br><span class="line">493</span><br><span class="line">494</span><br><span class="line">495</span><br><span class="line">496</span><br><span class="line">497</span><br><span class="line">498</span><br><span class="line">499</span><br><span class="line">500</span><br><span class="line">501</span><br><span class="line">502</span><br><span class="line">503</span><br><span class="line">504</span><br><span class="line">505</span><br><span class="line">506</span><br><span class="line">507</span><br><span class="line">508</span><br><span class="line">509</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> argparse</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> random</span><br><span class="line"><span class="keyword">import</span> shutil</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> warnings</span><br><span class="line"><span class="keyword">from</span> enum <span class="keyword">import</span> Enum</span><br><span class="line"><span class="keyword">import</span> torch</span><br><span class="line"><span class="keyword">import</span> torch.backends.cudnn <span class="keyword">as</span> cudnn</span><br><span class="line"><span class="keyword">import</span> torch.distributed <span class="keyword">as</span> dist</span><br><span class="line"><span class="keyword">import</span> torch.multiprocessing <span class="keyword">as</span> mp</span><br><span class="line"><span class="keyword">import</span> torch.nn <span class="keyword">as</span> nn</span><br><span class="line"><span class="keyword">import</span> torch.nn.parallel</span><br><span class="line"><span class="keyword">import</span> torch.optim</span><br><span class="line"><span class="keyword">import</span> torch.utils.data</span><br><span class="line"><span class="keyword">import</span> torch.utils.data.distributed</span><br><span class="line"><span class="keyword">import</span> torchvision.datasets <span class="keyword">as</span> datasets</span><br><span class="line"><span class="keyword">import</span> torchvision.models <span class="keyword">as</span> models</span><br><span class="line"><span class="keyword">import</span> torchvision.transforms <span class="keyword">as</span> transforms</span><br><span class="line"><span class="keyword">from</span> torch.optim.lr_scheduler <span class="keyword">import</span> StepLR</span><br><span class="line"><span class="keyword">from</span> torch.utils.data <span class="keyword">import</span> Subset</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">model_names = <span class="built_in">sorted</span>(name <span class="keyword">for</span> name <span class="keyword">in</span> models.__dict__</span><br><span class="line">                     <span class="keyword">if</span> name.islower() <span class="keyword">and</span> <span class="keyword">not</span> name.startswith(<span class="string">&quot;__&quot;</span>)</span><br><span class="line">                     <span class="keyword">and</span> <span class="built_in">callable</span>(models.__dict__[name]))</span><br><span class="line"></span><br><span class="line">parser = argparse.ArgumentParser(description=<span class="string">&#x27;PyTorch ImageNet Training&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;data&#x27;</span>, metavar=<span class="string">&#x27;DIR&#x27;</span>, nargs=<span class="string">&#x27;?&#x27;</span>, default=<span class="string">&#x27;imagenet&#x27;</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;path to dataset (default: imagenet)&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;-a&#x27;</span>, <span class="string">&#x27;--arch&#x27;</span>, metavar=<span class="string">&#x27;ARCH&#x27;</span>, default=<span class="string">&#x27;resnet34&#x27;</span>,</span><br><span class="line">                    choices=model_names,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;model architecture: &#x27;</span> +</span><br><span class="line">                         <span class="string">&#x27; | &#x27;</span>.join(model_names) +</span><br><span class="line">                         <span class="string">&#x27; (default: resnet34)&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;-j&#x27;</span>, <span class="string">&#x27;--workers&#x27;</span>, default=<span class="number">4</span>, <span class="built_in">type</span>=<span class="built_in">int</span>, metavar=<span class="string">&#x27;N&#x27;</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;number of data loading workers (default: 4)&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;--epochs&#x27;</span>, default=<span class="number">50</span>, <span class="built_in">type</span>=<span class="built_in">int</span>, metavar=<span class="string">&#x27;N&#x27;</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;number of total epochs to run&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;--start-epoch&#x27;</span>, default=<span class="number">1</span>, <span class="built_in">type</span>=<span class="built_in">int</span>, metavar=<span class="string">&#x27;N&#x27;</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;manual epoch number (useful on restarts)&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;-b&#x27;</span>, <span class="string">&#x27;--batch-size&#x27;</span>, default=<span class="number">192</span>, <span class="built_in">type</span>=<span class="built_in">int</span>,</span><br><span class="line">                    metavar=<span class="string">&#x27;N&#x27;</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;mini-batch size (default: 256), this is the total &#x27;</span></span><br><span class="line">                         <span class="string">&#x27;batch size of all GPUs on the current node when &#x27;</span></span><br><span class="line">                         <span class="string">&#x27;using Data Parallel or Distributed Data Parallel&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;--lr&#x27;</span>, <span class="string">&#x27;--learning-rate&#x27;</span>, default=<span class="number">0.1</span>, <span class="built_in">type</span>=<span class="built_in">float</span>,</span><br><span class="line">                    metavar=<span class="string">&#x27;LR&#x27;</span>, <span class="built_in">help</span>=<span class="string">&#x27;initial learning rate&#x27;</span>, dest=<span class="string">&#x27;lr&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;--momentum&#x27;</span>, default=<span class="number">0.9</span>, <span class="built_in">type</span>=<span class="built_in">float</span>, metavar=<span class="string">&#x27;M&#x27;</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;momentum&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;--wd&#x27;</span>, <span class="string">&#x27;--weight-decay&#x27;</span>, default=<span class="number">1e-4</span>, <span class="built_in">type</span>=<span class="built_in">float</span>,</span><br><span class="line">                    metavar=<span class="string">&#x27;W&#x27;</span>, <span class="built_in">help</span>=<span class="string">&#x27;weight decay (default: 1e-4)&#x27;</span>,</span><br><span class="line">                    dest=<span class="string">&#x27;weight_decay&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;-p&#x27;</span>, <span class="string">&#x27;--print-freq&#x27;</span>, default=<span class="number">500</span>, <span class="built_in">type</span>=<span class="built_in">int</span>,</span><br><span class="line">                    metavar=<span class="string">&#x27;N&#x27;</span>, <span class="built_in">help</span>=<span class="string">&#x27;print frequency (default: 500)&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;--resume&#x27;</span>, default=<span class="string">&#x27;&#x27;</span>, <span class="built_in">type</span>=<span class="built_in">str</span>, metavar=<span class="string">&#x27;PATH&#x27;</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;path to latest checkpoint (default: none)&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;-e&#x27;</span>, <span class="string">&#x27;--evaluate&#x27;</span>, dest=<span class="string">&#x27;evaluate&#x27;</span>, action=<span class="string">&#x27;store_true&#x27;</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;evaluate model on validation set&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;--pretrained&#x27;</span>, dest=<span class="string">&#x27;pretrained&#x27;</span>, action=<span class="string">&#x27;store_true&#x27;</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;use pre-trained model&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;--world-size&#x27;</span>, default=-<span class="number">1</span>, <span class="built_in">type</span>=<span class="built_in">int</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;number of nodes for distributed training&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;--rank&#x27;</span>, default=-<span class="number">1</span>, <span class="built_in">type</span>=<span class="built_in">int</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;node rank for distributed training&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;--dist-url&#x27;</span>, default=<span class="string">&#x27;tcp://224.66.41.62:23456&#x27;</span>, <span class="built_in">type</span>=<span class="built_in">str</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;url used to set up distributed training&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;--dist-backend&#x27;</span>, default=<span class="string">&#x27;nccl&#x27;</span>, <span class="built_in">type</span>=<span class="built_in">str</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;distributed backend&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;--seed&#x27;</span>, default=<span class="literal">None</span>, <span class="built_in">type</span>=<span class="built_in">int</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;seed for initializing training. &#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;--gpu&#x27;</span>, default=<span class="literal">None</span>, <span class="built_in">type</span>=<span class="built_in">int</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;GPU id to use.&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;--multiprocessing-distributed&#x27;</span>, action=<span class="string">&#x27;store_true&#x27;</span>,</span><br><span class="line">                    <span class="built_in">help</span>=<span class="string">&#x27;Use multi-processing distributed training to launch &#x27;</span></span><br><span class="line">                         <span class="string">&#x27;N processes per node, which has N GPUs. This is the &#x27;</span></span><br><span class="line">                         <span class="string">&#x27;fastest way to use PyTorch for either single node or &#x27;</span></span><br><span class="line">                         <span class="string">&#x27;multi node data parallel training&#x27;</span>)</span><br><span class="line">parser.add_argument(<span class="string">&#x27;--dummy&#x27;</span>, action=<span class="string">&#x27;store_true&#x27;</span>, <span class="built_in">help</span>=<span class="string">&quot;use fake data to benchmark&quot;</span>)</span><br><span class="line"></span><br><span class="line">best_acc1 = <span class="number">0</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">main</span>():</span><br><span class="line">    args = parser.parse_args()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> args.seed <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">        random.seed(args.seed)</span><br><span class="line">        torch.manual_seed(args.seed)</span><br><span class="line">        cudnn.deterministic = <span class="literal">True</span></span><br><span class="line">        cudnn.benchmark = <span class="literal">False</span></span><br><span class="line">        warnings.warn(<span class="string">&#x27;You have chosen to seed training. &#x27;</span></span><br><span class="line">                      <span class="string">&#x27;This will turn on the CUDNN deterministic setting, &#x27;</span></span><br><span class="line">                      <span class="string">&#x27;which can slow down your training considerably! &#x27;</span></span><br><span class="line">                      <span class="string">&#x27;You may see unexpected behavior when restarting &#x27;</span></span><br><span class="line">                      <span class="string">&#x27;from checkpoints.&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> args.gpu <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">        warnings.warn(<span class="string">&#x27;You have chosen a specific GPU. This will completely &#x27;</span></span><br><span class="line">                      <span class="string">&#x27;disable data parallelism.&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> args.dist_url == <span class="string">&quot;env://&quot;</span> <span class="keyword">and</span> args.world_size == -<span class="number">1</span>:</span><br><span class="line">        args.world_size = <span class="built_in">int</span>(os.environ[<span class="string">&quot;WORLD_SIZE&quot;</span>])</span><br><span class="line"></span><br><span class="line">    args.distributed = args.world_size &gt; <span class="number">1</span> <span class="keyword">or</span> args.multiprocessing_distributed</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> torch.cuda.is_available():</span><br><span class="line">        ngpus_per_node = torch.cuda.device_count()</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        ngpus_per_node = <span class="number">1</span></span><br><span class="line">    <span class="keyword">if</span> args.multiprocessing_distributed:</span><br><span class="line">        <span class="comment"># Since we have ngpus_per_node processes per node, the total world_size</span></span><br><span class="line">        <span class="comment"># needs to be adjusted accordingly</span></span><br><span class="line">        args.world_size = ngpus_per_node * args.world_size</span><br><span class="line">        <span class="comment"># Use torch.multiprocessing.spawn to launch distributed processes: the</span></span><br><span class="line">        <span class="comment"># main_worker process function</span></span><br><span class="line">        mp.spawn(main_worker, nprocs=ngpus_per_node, args=(ngpus_per_node, args))</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="comment"># Simply call main_worker function</span></span><br><span class="line">        main_worker(args.gpu, ngpus_per_node, args)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">main_worker</span>(<span class="params">gpu, ngpus_per_node, args</span>):</span><br><span class="line">    <span class="keyword">global</span> best_acc1</span><br><span class="line">    args.gpu = gpu</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> args.gpu <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;Use GPU: &#123;&#125; for training&quot;</span>.<span class="built_in">format</span>(args.gpu))</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> args.distributed:</span><br><span class="line">        <span class="keyword">if</span> args.dist_url == <span class="string">&quot;env://&quot;</span> <span class="keyword">and</span> args.rank == -<span class="number">1</span>:</span><br><span class="line">            args.rank = <span class="built_in">int</span>(os.environ[<span class="string">&quot;RANK&quot;</span>])</span><br><span class="line">        <span class="keyword">if</span> args.multiprocessing_distributed:</span><br><span class="line">            <span class="comment"># For multiprocessing distributed training, rank needs to be the</span></span><br><span class="line">            <span class="comment"># global rank among all the processes</span></span><br><span class="line">            args.rank = args.rank * ngpus_per_node + gpu</span><br><span class="line">        dist.init_process_group(backend=args.dist_backend, init_method=args.dist_url,</span><br><span class="line">                                world_size=args.world_size, rank=args.rank)</span><br><span class="line">    <span class="comment"># create model</span></span><br><span class="line">    <span class="keyword">if</span> args.pretrained:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;=&gt; using pre-trained model &#x27;&#123;&#125;&#x27;&quot;</span>.<span class="built_in">format</span>(args.arch))</span><br><span class="line">        model = models.__dict__[args.arch](pretrained=<span class="literal">True</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;=&gt; creating model &#x27;&#123;&#125;&#x27;&quot;</span>.<span class="built_in">format</span>(args.arch))</span><br><span class="line">        model = models.__dict__[args.arch]()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">not</span> torch.cuda.is_available() <span class="keyword">and</span> <span class="keyword">not</span> torch.backends.mps.is_available():</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;using CPU, this will be slow&#x27;</span>)</span><br><span class="line">    <span class="keyword">elif</span> args.distributed:</span><br><span class="line">        <span class="comment"># For multiprocessing distributed, DistributedDataParallel constructor</span></span><br><span class="line">        <span class="comment"># should always set the single device scope, otherwise,</span></span><br><span class="line">        <span class="comment"># DistributedDataParallel will use all available devices.</span></span><br><span class="line">        <span class="keyword">if</span> torch.cuda.is_available():</span><br><span class="line">            <span class="keyword">if</span> args.gpu <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">                torch.cuda.set_device(args.gpu)</span><br><span class="line">                model.cuda(args.gpu)</span><br><span class="line">                <span class="comment"># When using a single GPU per process and per</span></span><br><span class="line">                <span class="comment"># DistributedDataParallel, we need to divide the batch size</span></span><br><span class="line">                <span class="comment"># ourselves based on the total number of GPUs of the current node.</span></span><br><span class="line">                args.batch_size = <span class="built_in">int</span>(args.batch_size / ngpus_per_node)</span><br><span class="line">                args.workers = <span class="built_in">int</span>((args.workers + ngpus_per_node - <span class="number">1</span>) / ngpus_per_node)</span><br><span class="line">                model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu])</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                model.cuda()</span><br><span class="line">                <span class="comment"># DistributedDataParallel will divide and allocate batch_size to all</span></span><br><span class="line">                <span class="comment"># available GPUs if device_ids are not set</span></span><br><span class="line">                model = torch.nn.parallel.DistributedDataParallel(model)</span><br><span class="line">    <span class="keyword">elif</span> args.gpu <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span> <span class="keyword">and</span> torch.cuda.is_available():</span><br><span class="line">        torch.cuda.set_device(args.gpu)</span><br><span class="line">        model = model.cuda(args.gpu)</span><br><span class="line">    <span class="keyword">elif</span> torch.backends.mps.is_available():</span><br><span class="line">        device = torch.device(<span class="string">&quot;mps&quot;</span>)</span><br><span class="line">        model = model.to(device)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="comment"># DataParallel will divide and allocate batch_size to all available GPUs</span></span><br><span class="line">        <span class="keyword">if</span> args.arch.startswith(<span class="string">&#x27;alexnet&#x27;</span>) <span class="keyword">or</span> args.arch.startswith(<span class="string">&#x27;vgg&#x27;</span>):</span><br><span class="line">            model.features = torch.nn.DataParallel(model.features)</span><br><span class="line">            model.cuda()</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            model = torch.nn.DataParallel(model).cuda()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> torch.cuda.is_available():</span><br><span class="line">        <span class="keyword">if</span> args.gpu:</span><br><span class="line">            device = torch.device(<span class="string">&#x27;cuda:&#123;&#125;&#x27;</span>.<span class="built_in">format</span>(args.gpu))</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            device = torch.device(<span class="string">&quot;cuda&quot;</span>)</span><br><span class="line">    <span class="keyword">elif</span> torch.backends.mps.is_available():</span><br><span class="line">        device = torch.device(<span class="string">&quot;mps&quot;</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        device = torch.device(<span class="string">&quot;cpu&quot;</span>)</span><br><span class="line">    <span class="comment"># define loss function (criterion), optimizer, and learning rate scheduler</span></span><br><span class="line">    criterion = nn.CrossEntropyLoss().to(device)</span><br><span class="line"></span><br><span class="line">    optimizer = torch.optim.SGD(model.parameters(), args.lr,</span><br><span class="line">                                momentum=args.momentum,</span><br><span class="line">                                weight_decay=args.weight_decay)</span><br><span class="line"></span><br><span class="line">    <span class="string">&quot;&quot;&quot;Sets the learning rate to the initial LR decayed by 10 every 30 epochs&quot;&quot;&quot;</span></span><br><span class="line">    scheduler = StepLR(optimizer, step_size=<span class="number">30</span>, gamma=<span class="number">0.1</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># optionally resume from a checkpoint</span></span><br><span class="line">    <span class="keyword">if</span> args.resume:</span><br><span class="line">        <span class="keyword">if</span> os.path.isfile(args.resume):</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;=&gt; loading checkpoint &#x27;&#123;&#125;&#x27;&quot;</span>.<span class="built_in">format</span>(args.resume))</span><br><span class="line">            <span class="keyword">if</span> args.gpu <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">                checkpoint = torch.load(args.resume)</span><br><span class="line">            <span class="keyword">elif</span> torch.cuda.is_available():</span><br><span class="line">                <span class="comment"># Map model to be loaded to specified single gpu.</span></span><br><span class="line">                loc = <span class="string">&#x27;cuda:&#123;&#125;&#x27;</span>.<span class="built_in">format</span>(args.gpu)</span><br><span class="line">                checkpoint = torch.load(args.resume, map_location=loc)</span><br><span class="line">            args.start_epoch = checkpoint[<span class="string">&#x27;epoch&#x27;</span>]</span><br><span class="line">            best_acc1 = checkpoint[<span class="string">&#x27;best_acc1&#x27;</span>]</span><br><span class="line">            <span class="keyword">if</span> args.gpu <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">                <span class="comment"># best_acc1 may be from a checkpoint from a different GPU</span></span><br><span class="line">                best_acc1 = best_acc1.to(args.gpu)</span><br><span class="line">            model.load_state_dict(checkpoint[<span class="string">&#x27;state_dict&#x27;</span>])</span><br><span class="line">            optimizer.load_state_dict(checkpoint[<span class="string">&#x27;optimizer&#x27;</span>])</span><br><span class="line">            scheduler.load_state_dict(checkpoint[<span class="string">&#x27;scheduler&#x27;</span>])</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;=&gt; loaded checkpoint &#x27;&#123;&#125;&#x27; (epoch &#123;&#125;)&quot;</span></span><br><span class="line">                  .<span class="built_in">format</span>(args.resume, checkpoint[<span class="string">&#x27;epoch&#x27;</span>]))</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;=&gt; no checkpoint found at &#x27;&#123;&#125;&#x27;&quot;</span>.<span class="built_in">format</span>(args.resume))</span><br><span class="line"></span><br><span class="line">    <span class="comment"># Data loading code</span></span><br><span class="line">    <span class="keyword">if</span> args.dummy:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;=&gt; Dummy data is used!&quot;</span>)</span><br><span class="line">        train_dataset = datasets.FakeData(<span class="number">1281167</span>, (<span class="number">3</span>, <span class="number">224</span>, <span class="number">224</span>), <span class="number">1000</span>, transforms.ToTensor())</span><br><span class="line">        val_dataset = datasets.FakeData(<span class="number">50000</span>, (<span class="number">3</span>, <span class="number">224</span>, <span class="number">224</span>), <span class="number">1000</span>, transforms.ToTensor())</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        normalize = transforms.Normalize(mean=[<span class="number">0.485</span>, <span class="number">0.456</span>, <span class="number">0.406</span>],</span><br><span class="line">                                         std=[<span class="number">0.229</span>, <span class="number">0.224</span>, <span class="number">0.225</span>])</span><br><span class="line"></span><br><span class="line">        train_dataset = datasets.ImageFolder(</span><br><span class="line">            <span class="string">&quot;数据集的路径：&quot;</span>,</span><br><span class="line">            transforms.Compose([</span><br><span class="line">                transforms.RandomResizedCrop(<span class="number">224</span>),</span><br><span class="line">                transforms.RandomHorizontalFlip(),</span><br><span class="line">                transforms.ToTensor(),</span><br><span class="line">                normalize,</span><br><span class="line">            ]))</span><br><span class="line"></span><br><span class="line">        val_dataset = datasets.ImageFolder(</span><br><span class="line">            <span class="string">&quot;测试集的路径：&quot;</span>,</span><br><span class="line">            transforms.Compose([</span><br><span class="line">                transforms.Resize(<span class="number">256</span>),</span><br><span class="line">                transforms.CenterCrop(<span class="number">224</span>),</span><br><span class="line">                transforms.ToTensor(),</span><br><span class="line">                normalize,</span><br><span class="line">            ]))</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> args.distributed:</span><br><span class="line">        train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset)</span><br><span class="line">        val_sampler = torch.utils.data.distributed.DistributedSampler(val_dataset, shuffle=<span class="literal">False</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        train_sampler = <span class="literal">None</span></span><br><span class="line">        val_sampler = <span class="literal">None</span></span><br><span class="line"></span><br><span class="line">    train_loader = torch.utils.data.DataLoader(</span><br><span class="line">        train_dataset, batch_size=args.batch_size, shuffle=(train_sampler <span class="keyword">is</span> <span class="literal">None</span>),</span><br><span class="line">        num_workers=args.workers, pin_memory=<span class="literal">True</span>, sampler=train_sampler)</span><br><span class="line"></span><br><span class="line">    val_loader = torch.utils.data.DataLoader(</span><br><span class="line">        val_dataset, batch_size=args.batch_size, shuffle=<span class="literal">False</span>,</span><br><span class="line">        num_workers=args.workers, pin_memory=<span class="literal">True</span>, sampler=val_sampler)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> args.evaluate:</span><br><span class="line">        validate(val_loader, model, criterion, args)</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line"></span><br><span class="line">    end = time.time()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> epoch <span class="keyword">in</span> <span class="built_in">range</span>(args.start_epoch, args.epochs):</span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;\n-----第&#123;&#125;轮训练开始-----&quot;</span>.<span class="built_in">format</span>(epoch))</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> args.distributed:</span><br><span class="line">            train_sampler.set_epoch(epoch)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># train for one epoch</span></span><br><span class="line">        train(train_loader, model, criterion, optimizer, epoch, device, args, end)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># evaluate on validation set</span></span><br><span class="line">        acc1 = validate(val_loader, model, criterion, args, end)</span><br><span class="line"></span><br><span class="line">        scheduler.step()</span><br><span class="line"></span><br><span class="line">        <span class="comment"># remember best acc@1 and save checkpoint</span></span><br><span class="line">        is_best = acc1 &gt; best_acc1</span><br><span class="line">        best_acc1 = <span class="built_in">max</span>(acc1, best_acc1)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> args.multiprocessing_distributed <span class="keyword">or</span> (args.multiprocessing_distributed</span><br><span class="line">                                                    <span class="keyword">and</span> args.rank % ngpus_per_node == <span class="number">0</span>):</span><br><span class="line">            save_checkpoint(&#123;</span><br><span class="line">                <span class="string">&#x27;epoch&#x27;</span>: epoch + <span class="number">1</span>,</span><br><span class="line">                <span class="string">&#x27;arch&#x27;</span>: args.arch,</span><br><span class="line">                <span class="string">&#x27;state_dict&#x27;</span>: model.state_dict(),</span><br><span class="line">                <span class="string">&#x27;best_acc1&#x27;</span>: best_acc1,</span><br><span class="line">                <span class="string">&#x27;optimizer&#x27;</span>: optimizer.state_dict(),</span><br><span class="line">                <span class="string">&#x27;scheduler&#x27;</span>: scheduler.state_dict()</span><br><span class="line">            &#125;, is_best)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">train</span>(<span class="params">train_loader, model, criterion, optimizer, epoch, device, args, end</span>):</span><br><span class="line">    losses = AverageMeter(<span class="string">&#x27;Loss&#x27;</span>, <span class="string">&#x27;:.4e&#x27;</span>)</span><br><span class="line">    top1 = AverageMeter(<span class="string">&#x27;Acc@1&#x27;</span>, <span class="string">&#x27;:6.2f&#x27;</span>)</span><br><span class="line">    top5 = AverageMeter(<span class="string">&#x27;Acc@5&#x27;</span>, <span class="string">&#x27;:6.2f&#x27;</span>)</span><br><span class="line">    progress = ProgressMeter(</span><br><span class="line">        <span class="built_in">len</span>(train_loader),</span><br><span class="line">        [losses, top1, top5],</span><br><span class="line">        prefix=<span class="string">&quot;Epoch: [&#123;&#125;]&quot;</span>.<span class="built_in">format</span>(epoch))</span><br><span class="line"></span><br><span class="line">    <span class="comment"># switch to train mode</span></span><br><span class="line">    model.train()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> i, (images, target) <span class="keyword">in</span> <span class="built_in">enumerate</span>(train_loader):</span><br><span class="line">        <span class="comment"># measure data loading time</span></span><br><span class="line"></span><br><span class="line">        <span class="comment"># move data to the same device as model</span></span><br><span class="line">        images = images.to(device, non_blocking=<span class="literal">True</span>)</span><br><span class="line">        target = target.to(device, non_blocking=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># compute output</span></span><br><span class="line">        output = model(images)</span><br><span class="line">        loss = criterion(output, target)</span><br><span class="line"></span><br><span class="line">        <span class="comment"># measure accuracy and record loss</span></span><br><span class="line">        acc1, acc5 = accuracy(output, target, topk=(<span class="number">1</span>, <span class="number">5</span>))</span><br><span class="line">        losses.update(loss.item(), images.size(<span class="number">0</span>))</span><br><span class="line">        top1.update(acc1[<span class="number">0</span>], images.size(<span class="number">0</span>))</span><br><span class="line">        top5.update(acc5[<span class="number">0</span>], images.size(<span class="number">0</span>))</span><br><span class="line"></span><br><span class="line">        <span class="comment"># compute gradient and do SGD step</span></span><br><span class="line">        optimizer.zero_grad()</span><br><span class="line">        loss.backward()</span><br><span class="line">        optimizer.step()</span><br><span class="line"></span><br><span class="line">        <span class="comment"># measure elapsed time</span></span><br><span class="line">        <span class="keyword">if</span> i % args.print_freq == <span class="number">0</span>:</span><br><span class="line">            progress.display(i + <span class="number">1</span>)</span><br><span class="line">            <span class="built_in">print</span>(<span class="string">&quot;Time: %.2fs&quot;</span> % (time.time() - end))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">validate</span>(<span class="params">val_loader, model, criterion, args, end</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">run_validate</span>(<span class="params">loader, base_progress=<span class="number">0</span></span>):</span><br><span class="line">        <span class="keyword">with</span> torch.no_grad():</span><br><span class="line">            end = time.time()</span><br><span class="line">            <span class="keyword">for</span> i, (images, target) <span class="keyword">in</span> <span class="built_in">enumerate</span>(loader):</span><br><span class="line">                i = base_progress + i</span><br><span class="line">                <span class="keyword">if</span> args.gpu <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span> <span class="keyword">and</span> torch.cuda.is_available():</span><br><span class="line">                    images = images.cuda(args.gpu, non_blocking=<span class="literal">True</span>)</span><br><span class="line">                <span class="keyword">if</span> torch.backends.mps.is_available():</span><br><span class="line">                    images = images.to(<span class="string">&#x27;mps&#x27;</span>)</span><br><span class="line">                    target = target.to(<span class="string">&#x27;mps&#x27;</span>)</span><br><span class="line">                <span class="keyword">if</span> torch.cuda.is_available():</span><br><span class="line">                    target = target.cuda(args.gpu, non_blocking=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">                <span class="comment"># compute output</span></span><br><span class="line">                output = model(images)</span><br><span class="line">                loss = criterion(output, target)</span><br><span class="line"></span><br><span class="line">                <span class="comment"># measure accuracy and record loss</span></span><br><span class="line">                acc1, acc5 = accuracy(output, target, topk=(<span class="number">1</span>, <span class="number">5</span>))</span><br><span class="line">                losses.update(loss.item(), images.size(<span class="number">0</span>))</span><br><span class="line">                top1.update(acc1[<span class="number">0</span>], images.size(<span class="number">0</span>))</span><br><span class="line">                top5.update(acc5[<span class="number">0</span>], images.size(<span class="number">0</span>))</span><br><span class="line"></span><br><span class="line">                <span class="comment"># measure elapsed time</span></span><br><span class="line"></span><br><span class="line">                <span class="keyword">if</span> i % args.print_freq == <span class="number">0</span>:</span><br><span class="line">                    progress.display(i + <span class="number">1</span>)</span><br><span class="line">                    <span class="built_in">print</span>(<span class="string">&quot;time: %.1fs&quot;</span> % (time.time() - end))</span><br><span class="line"></span><br><span class="line">    losses = AverageMeter(<span class="string">&#x27;Loss&#x27;</span>, <span class="string">&#x27;:.4e&#x27;</span>, Summary.NONE)</span><br><span class="line">    top1 = AverageMeter(<span class="string">&#x27;Acc@1&#x27;</span>, <span class="string">&#x27;:6.2f&#x27;</span>, Summary.AVERAGE)</span><br><span class="line">    top5 = AverageMeter(<span class="string">&#x27;Acc@5&#x27;</span>, <span class="string">&#x27;:6.2f&#x27;</span>, Summary.AVERAGE)</span><br><span class="line">    progress = ProgressMeter(</span><br><span class="line">        <span class="built_in">len</span>(val_loader) + (args.distributed <span class="keyword">and</span> (<span class="built_in">len</span>(val_loader.sampler) * args.world_size &lt; <span class="built_in">len</span>(val_loader.dataset))),</span><br><span class="line">        [losses, top1, top5],</span><br><span class="line">        prefix=<span class="string">&#x27;Test: &#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># switch to evaluate mode</span></span><br><span class="line">    model.<span class="built_in">eval</span>()</span><br><span class="line"></span><br><span class="line">    run_validate(val_loader)</span><br><span class="line">    <span class="keyword">if</span> args.distributed:</span><br><span class="line">        top1.all_reduce()</span><br><span class="line">        top5.all_reduce()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> args.distributed <span class="keyword">and</span> (<span class="built_in">len</span>(val_loader.sampler) * args.world_size &lt; <span class="built_in">len</span>(val_loader.dataset)):</span><br><span class="line">        aux_val_dataset = Subset(val_loader.dataset,</span><br><span class="line">                                 <span class="built_in">range</span>(<span class="built_in">len</span>(val_loader.sampler) * args.world_size, <span class="built_in">len</span>(val_loader.dataset)))</span><br><span class="line">        aux_val_loader = torch.utils.data.DataLoader(</span><br><span class="line">            aux_val_dataset, batch_size=args.batch_size, shuffle=<span class="literal">False</span>,</span><br><span class="line">            num_workers=args.workers, pin_memory=<span class="literal">True</span>)</span><br><span class="line">        run_validate(aux_val_loader, <span class="built_in">len</span>(val_loader))</span><br><span class="line"></span><br><span class="line">    progress.display_summary()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> top1.avg</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">save_checkpoint</span>(<span class="params">state, is_best, filename=<span class="string">&#x27;../models/checkpoint_resnet34.pth.tar&#x27;</span></span>):</span><br><span class="line">    torch.save(state, filename)</span><br><span class="line">    <span class="keyword">if</span> is_best:</span><br><span class="line">        shutil.copyfile(filename, <span class="string">&#x27;../models/model_best_resnet34.pth.tar&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Summary</span>(<span class="title class_ inherited__">Enum</span>):</span><br><span class="line">    NONE = <span class="number">0</span></span><br><span class="line">    AVERAGE = <span class="number">1</span></span><br><span class="line">    SUM = <span class="number">2</span></span><br><span class="line">    COUNT = <span class="number">3</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">AverageMeter</span>(<span class="title class_ inherited__">object</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;Computes and stores the average and current value&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, name, fmt=<span class="string">&#x27;:f&#x27;</span>, summary_type=Summary.AVERAGE</span>):</span><br><span class="line">        self.name = name</span><br><span class="line">        self.fmt = fmt</span><br><span class="line">        self.summary_type = summary_type</span><br><span class="line">        self.reset()</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">reset</span>(<span class="params">self</span>):</span><br><span class="line">        self.val = <span class="number">0</span></span><br><span class="line">        self.avg = <span class="number">0</span></span><br><span class="line">        self.<span class="built_in">sum</span> = <span class="number">0</span></span><br><span class="line">        self.count = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">update</span>(<span class="params">self, val, n=<span class="number">1</span></span>):</span><br><span class="line">        self.val = val</span><br><span class="line">        self.<span class="built_in">sum</span> += val * n</span><br><span class="line">        self.count += n</span><br><span class="line">        self.avg = self.<span class="built_in">sum</span> / self.count</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">all_reduce</span>(<span class="params">self</span>):</span><br><span class="line">        <span class="keyword">if</span> torch.cuda.is_available():</span><br><span class="line">            device = torch.device(<span class="string">&quot;cuda&quot;</span>)</span><br><span class="line">        <span class="keyword">elif</span> torch.backends.mps.is_available():</span><br><span class="line">            device = torch.device(<span class="string">&quot;mps&quot;</span>)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            device = torch.device(<span class="string">&quot;cpu&quot;</span>)</span><br><span class="line">        total = torch.tensor([self.<span class="built_in">sum</span>, self.count], dtype=torch.float32, device=device)</span><br><span class="line">        dist.all_reduce(total, dist.ReduceOp.SUM, async_op=<span class="literal">False</span>)</span><br><span class="line">        self.<span class="built_in">sum</span>, self.count = total.tolist()</span><br><span class="line">        self.avg = self.<span class="built_in">sum</span> / self.count</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__str__</span>(<span class="params">self</span>):</span><br><span class="line">        fmtstr = <span class="string">&#x27;&#123;name&#125; &#123;val&#x27;</span> + self.fmt + <span class="string">&#x27;&#125; (&#123;avg&#x27;</span> + self.fmt + <span class="string">&#x27;&#125;)&#x27;</span></span><br><span class="line">        <span class="keyword">return</span> fmtstr.<span class="built_in">format</span>(**self.__dict__)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">summary</span>(<span class="params">self</span>):</span><br><span class="line">        fmtstr = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="keyword">if</span> self.summary_type <span class="keyword">is</span> Summary.NONE:</span><br><span class="line">            fmtstr = <span class="string">&#x27;&#x27;</span></span><br><span class="line">        <span class="keyword">elif</span> self.summary_type <span class="keyword">is</span> Summary.AVERAGE:</span><br><span class="line">            fmtstr = <span class="string">&#x27;&#123;name&#125; &#123;avg:.3f&#125;&#x27;</span></span><br><span class="line">        <span class="keyword">elif</span> self.summary_type <span class="keyword">is</span> Summary.SUM:</span><br><span class="line">            fmtstr = <span class="string">&#x27;&#123;name&#125; &#123;sum:.3f&#125;&#x27;</span></span><br><span class="line">        <span class="keyword">elif</span> self.summary_type <span class="keyword">is</span> Summary.COUNT:</span><br><span class="line">            fmtstr = <span class="string">&#x27;&#123;name&#125; &#123;count:.3f&#125;&#x27;</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">raise</span> ValueError(<span class="string">&#x27;invalid summary type %r&#x27;</span> % self.summary_type)</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> fmtstr.<span class="built_in">format</span>(**self.__dict__)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">ProgressMeter</span>(<span class="title class_ inherited__">object</span>):</span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, num_batches, meters, prefix=<span class="string">&quot;&quot;</span></span>):</span><br><span class="line">        self.batch_fmtstr = self._get_batch_fmtstr(num_batches)</span><br><span class="line">        self.meters = meters</span><br><span class="line">        self.prefix = prefix</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">display</span>(<span class="params">self, batch</span>):</span><br><span class="line">        entries = [self.prefix + self.batch_fmtstr.<span class="built_in">format</span>(batch)]</span><br><span class="line">        entries += [<span class="built_in">str</span>(meter) <span class="keyword">for</span> meter <span class="keyword">in</span> self.meters]</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;\t&#x27;</span>.join(entries))</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">display_summary</span>(<span class="params">self</span>):</span><br><span class="line">        entries = [<span class="string">&quot; *&quot;</span>]</span><br><span class="line">        entries += [meter.summary() <span class="keyword">for</span> meter <span class="keyword">in</span> self.meters]</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27; &#x27;</span>.join(entries))</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">_get_batch_fmtstr</span>(<span class="params">self, num_batches</span>):</span><br><span class="line">        num_digits = <span class="built_in">len</span>(<span class="built_in">str</span>(num_batches // <span class="number">1</span>))</span><br><span class="line">        fmt = <span class="string">&#x27;&#123;:&#x27;</span> + <span class="built_in">str</span>(num_digits) + <span class="string">&#x27;d&#125;&#x27;</span></span><br><span class="line">        <span class="keyword">return</span> <span class="string">&#x27;[&#x27;</span> + fmt + <span class="string">&#x27;/&#x27;</span> + fmt.<span class="built_in">format</span>(num_batches) + <span class="string">&#x27;]&#x27;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">accuracy</span>(<span class="params">output, target, topk=(<span class="params"><span class="number">1</span>,</span>)</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;Computes the accuracy over the k top predictions for the specified values of k&quot;&quot;&quot;</span></span><br><span class="line">    <span class="keyword">with</span> torch.no_grad():</span><br><span class="line">        maxk = <span class="built_in">max</span>(topk)</span><br><span class="line">        batch_size = target.size(<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line">        _, pred = output.topk(maxk, <span class="number">1</span>, <span class="literal">True</span>, <span class="literal">True</span>)</span><br><span class="line">        pred = pred.t()</span><br><span class="line">        correct = pred.eq(target.view(<span class="number">1</span>, -<span class="number">1</span>).expand_as(pred))</span><br><span class="line"></span><br><span class="line">        res = []</span><br><span class="line">        <span class="keyword">for</span> k <span class="keyword">in</span> topk:</span><br><span class="line">            correct_k = correct[:k].reshape(-<span class="number">1</span>).<span class="built_in">float</span>().<span class="built_in">sum</span>(<span class="number">0</span>, keepdim=<span class="literal">True</span>)</span><br><span class="line">            res.append(correct_k.mul_(<span class="number">100.0</span> / batch_size))</span><br><span class="line">        <span class="keyword">return</span> res</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    main()</span><br><span class="line">    </span><br></pre></td></tr></table></figure>
<h2 id="三、小实践：可食用蘑菇识别分类模型">三、小实践：可食用蘑菇识别分类模型</h2>
<h3 id="训练代码：">训练代码：</h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> torch.nn</span><br><span class="line"><span class="keyword">import</span> torchvision</span><br><span class="line"><span class="keyword">import</span> torch.optim <span class="keyword">as</span> optim</span><br><span class="line"><span class="keyword">import</span> torchvision.transforms <span class="keyword">as</span> transforms</span><br><span class="line"><span class="keyword">from</span> torch <span class="keyword">import</span> nn</span><br><span class="line"><span class="keyword">from</span> torch.utils.tensorboard <span class="keyword">import</span> SummaryWriter</span><br><span class="line"><span class="keyword">from</span> torch.utils.data <span class="keyword">import</span> DataLoader</span><br><span class="line"></span><br><span class="line"><span class="comment"># 抛出错误数据</span></span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> ImageFile</span><br><span class="line">ImageFile.LOAD_TRUNCATED_IMAGES = <span class="literal">True</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置参量</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">weight_init</span>(<span class="params">m</span>):</span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">isinstance</span>(m, nn.Linear):</span><br><span class="line">        nn.init.xavier_normal_(m.weight)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">elif</span> <span class="built_in">isinstance</span>(m, nn.Conv2d):</span><br><span class="line">        nn.init.kaiming_normal_(m.weight, mode=<span class="string">&#x27;fan_out&#x27;</span>, nonlinearity=<span class="string">&#x27;relu&#x27;</span>)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">elif</span> <span class="built_in">isinstance</span>(m, nn.BatchNorm2d):</span><br><span class="line">        nn.init.constant_(m.weight, <span class="number">1</span>)</span><br><span class="line">        nn.init.constant_(m.bias, <span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 主训练函数</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">train</span>():</span><br><span class="line">    transform = transforms.Compose([</span><br><span class="line">        transforms.Resize(<span class="number">299</span>),</span><br><span class="line">        transforms.CenterCrop(<span class="number">224</span>),</span><br><span class="line">        transforms.ToTensor(),</span><br><span class="line">        transforms.Normalize(mean=[<span class="number">0.485</span>, <span class="number">0.456</span>, <span class="number">0.406</span>], std=[<span class="number">0.229</span>, <span class="number">0.224</span>, <span class="number">0.225</span>]),</span><br><span class="line">    ])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 数据集路径</span></span><br><span class="line">    train_set = torchvision.datasets.ImageFolder(root=<span class="string">&quot;E:\DeepLearning数据集\\archive (1)\\train&quot;</span>, transform=transform)</span><br><span class="line">    test_set = torchvision.datasets.ImageFolder(root=<span class="string">&quot;E:\DeepLearning数据集\\archive (1)\\test&quot;</span>, transform=transform)</span><br><span class="line"></span><br><span class="line">    train_loader = torch.utils.data.DataLoader(train_set, batch_size=<span class="number">64</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">True</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line">    test_loader = torch.utils.data.DataLoader(test_set, batch_size=<span class="number">64</span>, num_workers=<span class="number">4</span>, shuffle=<span class="literal">False</span>, pin_memory=<span class="literal">True</span>, drop_last=<span class="literal">True</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 写入tensorboard</span></span><br><span class="line">    writer = SummaryWriter(<span class="string">&quot;../train_ResNet_mushrooms&quot;</span>)</span><br><span class="line"></span><br><span class="line">    train_data_size = <span class="built_in">len</span>(train_set)</span><br><span class="line">    test_data_size = <span class="built_in">len</span>(test_set)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;训练数据集的长度为：&#123;&#125;&quot;</span>.<span class="built_in">format</span>(train_data_size))</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;测试数据集的长度为：&#123;&#125; \n&quot;</span>.<span class="built_in">format</span>(test_data_size))</span><br><span class="line"></span><br><span class="line">    device = torch.device(<span class="string">&quot;cuda&quot;</span> <span class="keyword">if</span> torch.cuda.is_available() <span class="keyword">else</span> <span class="string">&quot;cpu&quot;</span>)</span><br><span class="line">    model = torchvision.models.resnet18()</span><br><span class="line">    model.to(device)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># model.apply(weight_init)</span></span><br><span class="line"></span><br><span class="line">    <span class="comment"># 记录训练的轮数</span></span><br><span class="line">    total_train_step = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 记录测试的次数</span></span><br><span class="line">    total_test_step = <span class="number">0</span></span><br><span class="line">    <span class="comment"># 控制训练轮数</span></span><br><span class="line">    epochs = <span class="number">50</span></span><br><span class="line"></span><br><span class="line">    optimizer = optim.SGD(model.parameters(), lr=<span class="number">1e-3</span>, momentum=<span class="number">0.9</span>, weight_decay=<span class="number">0.0005</span>)</span><br><span class="line">    criteon = nn.CrossEntropyLoss().to(device)</span><br><span class="line">    scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=<span class="number">100</span>, eta_min=<span class="number">0.0001</span>, last_epoch=-<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">    start_time = time.time()</span><br><span class="line">    <span class="keyword">for</span> epoch <span class="keyword">in</span> <span class="built_in">range</span>(epochs):</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;-----第&#123;&#125;轮训练开始-----&quot;</span>.<span class="built_in">format</span>(epoch + <span class="number">1</span>))</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 训练开始</span></span><br><span class="line">        model.train()</span><br><span class="line">        <span class="keyword">global</span> train_loss</span><br><span class="line">        train_loss = <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> idx, (inputs, label) <span class="keyword">in</span> <span class="built_in">enumerate</span>(train_loader):</span><br><span class="line">            optimizer.zero_grad()</span><br><span class="line">            inputs, label = inputs.to(device), label.to(device)</span><br><span class="line">            outputs = model(inputs)</span><br><span class="line">            loss = criteon(outputs, label)</span><br><span class="line">            loss.backward()</span><br><span class="line">            optimizer.step()</span><br><span class="line">            train_loss += loss.item()</span><br><span class="line">            total_train_step += <span class="number">1</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;train_batchs:&#123;&#125;&quot;</span>.<span class="built_in">format</span>(total_train_step))</span><br><span class="line">        end_time = time.time()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;time: %.1fs&quot;</span> % (end_time - start_time))</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;train_loss:%.5f\n&quot;</span> % (train_loss / <span class="built_in">len</span>(train_set) * <span class="number">256</span>))</span><br><span class="line"></span><br><span class="line">        <span class="comment"># 测试开始</span></span><br><span class="line">        model.<span class="built_in">eval</span>()</span><br><span class="line">        <span class="keyword">global</span> test_loss, correct</span><br><span class="line">        test_loss = <span class="number">0</span></span><br><span class="line">        correct = <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> idx, (inputs, label) <span class="keyword">in</span> <span class="built_in">enumerate</span>(test_loader):</span><br><span class="line">            <span class="keyword">with</span> torch.no_grad():</span><br><span class="line">                inputs, label = inputs.to(device), label.to(device)</span><br><span class="line">                outputs = model(inputs)</span><br><span class="line">                test_loss += criteon(outputs, label)</span><br><span class="line">                predict = torch.<span class="built_in">max</span>(outputs, dim=<span class="number">1</span>)[<span class="number">1</span>]</span><br><span class="line">                correct += torch.eq(predict, label).<span class="built_in">sum</span>().item()</span><br><span class="line">                total_test_step += <span class="number">1</span></span><br><span class="line"></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;test_batchs:&#123;&#125;&quot;</span>.<span class="built_in">format</span>(total_test_step))</span><br><span class="line">        end_time = time.time()</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;time: %.1fs&quot;</span> % (end_time - start_time))</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;test_loss:%.5f&quot;</span> % (test_loss * <span class="number">256</span> / <span class="built_in">len</span>(test_set)))</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;test_acc:&#123;&#125;%\n&quot;</span>.<span class="built_in">format</span>((correct / <span class="built_in">len</span>(test_set) * <span class="number">100.0</span>), <span class="string">&#x27;.2f&#x27;</span>))</span><br><span class="line"></span><br><span class="line">        writer.add_scalar(<span class="string">&#x27;loss&#x27;</span>, train_loss / <span class="built_in">len</span>(train_loader), epoch)</span><br><span class="line">        writer.add_scalar(<span class="string">&#x27;acc&#x27;</span>, correct / <span class="built_in">len</span>(test_set), epoch)</span><br><span class="line">        scheduler.step()</span><br><span class="line"></span><br><span class="line">    torch.save(model, <span class="string">&#x27;../models/ResNet_mushrooms.pth&#x27;</span>)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&#x27;---Finished Training---&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    writer.close()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    train()</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="测试代码：">测试代码：</h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> torch</span><br><span class="line"><span class="keyword">import</span> torch.nn</span><br><span class="line"><span class="keyword">import</span> torchvision</span><br><span class="line"><span class="keyword">import</span> torchvision.transforms <span class="keyword">as</span> transforms</span><br><span class="line"><span class="keyword">from</span> torch.utils.tensorboard <span class="keyword">import</span> SummaryWriter</span><br><span class="line"><span class="keyword">from</span> torch.utils.data <span class="keyword">import</span> DataLoader</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> PIL <span class="keyword">import</span> ImageFile</span><br><span class="line">ImageFile.LOAD_TRUNCATED_IMAGES = <span class="literal">True</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">test</span>():</span><br><span class="line">    <span class="comment"># 转换输入图像</span></span><br><span class="line">    transform = transforms.Compose([</span><br><span class="line">            transforms.Resize(<span class="number">299</span>),</span><br><span class="line">            transforms.CenterCrop(<span class="number">224</span>),</span><br><span class="line">            transforms.ToTensor(),</span><br><span class="line">            transforms.Normalize(mean=[<span class="number">0.485</span>, <span class="number">0.456</span>, <span class="number">0.406</span>], std=[<span class="number">0.229</span>, <span class="number">0.224</span>, <span class="number">0.225</span>]),</span><br><span class="line">        ])</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 加载测试集</span></span><br><span class="line">    test_set = torchvision.datasets.ImageFolder(root=<span class="string">&quot;E:\DeepLearning数据集\\archive (1)\\test&quot;</span>, transform=transform)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 创建数据加载器</span></span><br><span class="line">    data_loader = torch.utils.data.DataLoader(test_set, batch_size=<span class="number">1</span>, num_workers=<span class="number">4</span>)</span><br><span class="line"></span><br><span class="line">    device = torch.device(<span class="string">&quot;cpu&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 加载模型</span></span><br><span class="line">    model = torch.load(<span class="string">&quot;..\models\\ResNet_mushrooms.pth&quot;</span>)</span><br><span class="line">    model.to(device)</span><br><span class="line">    model.<span class="built_in">eval</span>()</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 进行预测</span></span><br><span class="line">    correct = <span class="number">0</span></span><br><span class="line">    <span class="built_in">list</span> = [<span class="string">&#x27;Agaricus&#x27;</span>,<span class="string">&#x27;Amanita&#x27;</span>,<span class="string">&#x27;Boletus&#x27;</span>,<span class="string">&#x27;Cortinarius&#x27;</span>,<span class="string">&#x27;Entoloma&#x27;</span>,<span class="string">&#x27;Hygrocybe&#x27;</span>,<span class="string">&#x27;Lactarius&#x27;</span>,<span class="string">&#x27;Russula&#x27;</span>,<span class="string">&#x27;Suillus&#x27;</span>]</span><br><span class="line">    <span class="keyword">for</span> idx, (inputs, label) <span class="keyword">in</span> <span class="built_in">enumerate</span>(data_loader):</span><br><span class="line">        <span class="keyword">with</span> torch.no_grad():</span><br><span class="line">            inputs, label = inputs.to(device), label.to(device)</span><br><span class="line">            outputs = model(inputs)</span><br><span class="line">            predict = torch.<span class="built_in">max</span>(outputs, dim=<span class="number">1</span>)[<span class="number">1</span>]</span><br><span class="line">            correct += torch.eq(predict, label).<span class="built_in">sum</span>().item()</span><br><span class="line">            <span class="keyword">if</span> torch.<span class="built_in">max</span>(outputs) &lt;= <span class="number">6</span>:</span><br><span class="line">                <span class="built_in">print</span>(<span class="string">&#x27;drug&#x27;</span>)</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                <span class="built_in">print</span>(<span class="built_in">list</span>[predict])</span><br><span class="line">            correct += torch.eq(predict, label).<span class="built_in">sum</span>().item()</span><br><span class="line">            <span class="built_in">print</span>(outputs)</span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(correct)</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;test_acc:&#123;&#125;%\n&quot;</span>.<span class="built_in">format</span>((correct / <span class="built_in">len</span>(test_set) * <span class="number">100.0</span>), <span class="string">&#x27;.2f&#x27;</span>))</span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;Success!&quot;</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&quot;__main__&quot;</span>:</span><br><span class="line">    test()</span><br><span class="line">    </span><br></pre></td></tr></table></figure>
<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="https://lwkblog.gitee.io">1025</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="https://lwkblog.gitee.io/2023/03/15/Pytorch%E6%A1%86%E6%9E%B6%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E5%9B%BE%E5%83%8F%E5%88%86%E7%B1%BB%E6%A8%A1%E5%9E%8B/">https://lwkblog.gitee.io/2023/03/15/Pytorch%E6%A1%86%E6%9E%B6%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E5%9B%BE%E5%83%8F%E5%88%86%E7%B1%BB%E6%A8%A1%E5%9E%8B/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"></div><div class="post_share"><div class="social-share" data-image="https://pic1.zhimg.com/v2-5ea151d43c0ebb13546985d225ac256a_1200x500.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> Donate</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/img/wechat.jpg" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li></ul></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> Comment</span></div><div id="comment-switch"><span class="first-comment">Valine</span><span class="switch-btn"></span><span class="second-comment">Disqus</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div><div><div id="disqus_thread"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://pic1.zhimg.com/50/v2-530bac941983a6aae866ab34b6290be2_720w.jpg?source=1940ef5c" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">1025</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">12</div></a><a href="/tags/"><div class="headline">Tags</div><div class="length-num">0</div></a><a href="/categories/"><div class="headline">Categories</div><div class="length-num">5</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://codeforces.com/profile/2607563994"><i class="fa-solid fa-code-compare"></i><span>MyCodeforces</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://gitee.com/qq2607563994" target="_blank" title="MyGit"><i class="fa-brands fa-git-alt"></i></a><a class="social-icon" href="https://www.acwing.com/user/myspace/index/226518/" target="_blank" title="MyAcwing"><i class="fa-solid fa-font"></i></a><a class="social-icon" href="https://www.nowcoder.com/" target="_blank" title="poo"><i class="fa-solid fa-poo"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>Announcement</span></div><div class="announcement_content">Welcome to my blog, here is the world of learning~</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%A6%96%E5%85%88%E5%A3%B0%E6%98%8E%EF%BC%81%EF%BC%81%EF%BC%81"><span class="toc-number">1.</span> <span class="toc-text">首先声明！！！</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E7%BB%8F%E5%85%B8%E5%9B%BE%E5%83%8F%E5%88%86%E7%B1%BB%E6%95%B0%E6%8D%AE%E9%9B%86%EF%BC%9A"><span class="toc-number">2.</span> <span class="toc-text">一、经典图像分类数据集：</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%EF%BC%9Apytorch%E5%AE%98%E7%BD%91%E6%95%B0%E6%8D%AE%E9%9B%86-Datasets-%E2%80%94-Torchvision-0-15-documentation-pytorch-org"><span class="toc-number">2.1.</span> <span class="toc-text">注：pytorch官网数据集 Datasets — Torchvision 0.15 documentation (pytorch.org)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-CIFAR10"><span class="toc-number">2.2.</span> <span class="toc-text">1.CIFAR10</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-CIFAR100"><span class="toc-number">2.3.</span> <span class="toc-text">2.CIFAR100</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-ImageNet-ILSVRC2012"><span class="toc-number">2.4.</span> <span class="toc-text">3.ImageNet (ILSVRC2012)</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%9B%BE%E5%83%8F%E5%88%86%E7%B1%BB%E7%BB%8F%E5%85%B8%E6%A8%A1%E5%9E%8B%EF%BC%88%E5%9F%BA%E4%BA%8ECIFAR10%E6%95%B0%E6%8D%AE%E9%9B%86%EF%BC%89%EF%BC%9A"><span class="toc-number">3.</span> <span class="toc-text">二、图像分类经典模型（基于CIFAR10数据集）：</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%EF%BC%9A%E7%A1%AC%E4%BB%B6%E9%85%8D%E7%BD%AE%E6%98%BE%E5%AD%98%E6%9C%80%E5%A5%BD%E6%9C%8912g%EF%BC%8C%E6%A0%B9%E6%8D%AE%E6%98%BE%E5%AD%98%E5%A4%A7%E5%B0%8F%E8%B0%83%E6%95%B4batch-size%E7%9A%84%E5%A4%A7%E5%B0%8F%E3%80%82"><span class="toc-number">3.1.</span> <span class="toc-text">注：硬件配置显存最好有12g，根据显存大小调整batch_size的大小。</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-AlexNet%EF%BC%88%E8%87%AA%E5%B7%B1%E5%86%99%E7%9A%84%E6%A8%A1%E5%9E%8B%EF%BC%89"><span class="toc-number">3.2.</span> <span class="toc-text">1.AlexNet（自己写的模型）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-VGG%EF%BC%88models-vgg16-%EF%BC%89"><span class="toc-number">3.3.</span> <span class="toc-text">2.VGG（models.vgg16()）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-GoogLeNet"><span class="toc-number">3.4.</span> <span class="toc-text">3.GoogLeNet</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-ResNet%EF%BC%88models-resnet101-%EF%BC%89"><span class="toc-number">3.5.</span> <span class="toc-text">4.ResNet（models.resnet101()）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-DenseNet%EF%BC%88models-densenet121-%EF%BC%89"><span class="toc-number">3.6.</span> <span class="toc-text">5.DenseNet（models.densenet121()）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-Train-ImageNet%EF%BC%88main-py%EF%BC%89"><span class="toc-number">3.7.</span> <span class="toc-text">6.Train_ImageNet（main.py）</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E3%80%81%E5%B0%8F%E5%AE%9E%E8%B7%B5%EF%BC%9A%E5%8F%AF%E9%A3%9F%E7%94%A8%E8%98%91%E8%8F%87%E8%AF%86%E5%88%AB%E5%88%86%E7%B1%BB%E6%A8%A1%E5%9E%8B"><span class="toc-number">4.</span> <span class="toc-text">三、小实践：可食用蘑菇识别分类模型</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AE%AD%E7%BB%83%E4%BB%A3%E7%A0%81%EF%BC%9A"><span class="toc-number">4.1.</span> <span class="toc-text">训练代码：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E4%BB%A3%E7%A0%81%EF%BC%9A"><span class="toc-number">4.2.</span> <span class="toc-text">测试代码：</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2024/03/07/Codeforces%201900-2200/" title="Codeforces 1900-2200"><img src="https://th.bing.com/th/id/R.8764d162d1e9be6b2cf6d348e2da99f0?rik=TLZjyw4Nspa%2b0w&amp;riu=http%3a%2f%2fpic.616pic.com%2fys_bnew_img%2f00%2f62%2f36%2fr9dfcyoyjR.jpg&amp;ehk=SdJuWe8fxNWlX58TeKImDSWatngZpwGh6ann2DJ%2fXN0%3d&amp;risl=&amp;pid=ImgRaw&amp;r=0" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Codeforces 1900-2200"/></a><div class="content"><a class="title" href="/2024/03/07/Codeforces%201900-2200/" title="Codeforces 1900-2200">Codeforces 1900-2200</a><time datetime="2024-03-07T12:28:21.000Z" title="Created 2024-03-07 20:28:21">2024-03-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/12/22/%E8%AE%B0%E5%BD%95%E7%AC%AC%E4%B8%80%E6%AC%A1%E6%89%93icpc%E5%8C%BA%E5%9F%9F%E8%B5%9B%E7%9A%84%E6%84%9F%E5%8F%97/" title="记录第一次打icpc区域赛的感受"><img src="https://tse1-mm.cn.bing.net/th/id/OIP-C.3HHSbVgysU3bljjMphclBwHaE8?rs=1&amp;pid=ImgDetMain" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="记录第一次打icpc区域赛的感受"/></a><div class="content"><a class="title" href="/2023/12/22/%E8%AE%B0%E5%BD%95%E7%AC%AC%E4%B8%80%E6%AC%A1%E6%89%93icpc%E5%8C%BA%E5%9F%9F%E8%B5%9B%E7%9A%84%E6%84%9F%E5%8F%97/" title="记录第一次打icpc区域赛的感受">记录第一次打icpc区域赛的感受</a><time datetime="2023-12-22T00:06:41.000Z" title="Created 2023-12-22 08:06:41">2023-12-22</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/12/19/2023%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%8D%8F%E4%BC%9A%E7%BC%96%E7%A8%8B%E5%A4%A7%E8%B5%9B%E9%A2%98%E8%A7%A3%E4%BB%A3%E7%A0%81/" title="HLC_2023年计算机协会编程大赛题解代码"><img src="https://th.bing.com/th/id/R.8764d162d1e9be6b2cf6d348e2da99f0?rik=TLZjyw4Nspa%2b0w&amp;riu=http%3a%2f%2fpic.616pic.com%2fys_bnew_img%2f00%2f62%2f36%2fr9dfcyoyjR.jpg&amp;ehk=SdJuWe8fxNWlX58TeKImDSWatngZpwGh6ann2DJ%2fXN0%3d&amp;risl=&amp;pid=ImgRaw&amp;r=0" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="HLC_2023年计算机协会编程大赛题解代码"/></a><div class="content"><a class="title" href="/2023/12/19/2023%E8%AE%A1%E7%AE%97%E6%9C%BA%E5%8D%8F%E4%BC%9A%E7%BC%96%E7%A8%8B%E5%A4%A7%E8%B5%9B%E9%A2%98%E8%A7%A3%E4%BB%A3%E7%A0%81/" title="HLC_2023年计算机协会编程大赛题解代码">HLC_2023年计算机协会编程大赛题解代码</a><time datetime="2023-12-19T00:06:41.000Z" title="Created 2023-12-19 08:06:41">2023-12-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/06/03/Codeforces%201500-1800/" title="Codeforces 1500-1800"><img src="https://th.bing.com/th/id/R.8764d162d1e9be6b2cf6d348e2da99f0?rik=TLZjyw4Nspa%2b0w&amp;riu=http%3a%2f%2fpic.616pic.com%2fys_bnew_img%2f00%2f62%2f36%2fr9dfcyoyjR.jpg&amp;ehk=SdJuWe8fxNWlX58TeKImDSWatngZpwGh6ann2DJ%2fXN0%3d&amp;risl=&amp;pid=ImgRaw&amp;r=0" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Codeforces 1500-1800"/></a><div class="content"><a class="title" href="/2023/06/03/Codeforces%201500-1800/" title="Codeforces 1500-1800">Codeforces 1500-1800</a><time datetime="2023-06-03T12:28:21.000Z" title="Created 2023-06-03 20:28:21">2023-06-03</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/06/02/Codeforces%201100-1400/" title="Codeforces 1100-1400"><img src="https://th.bing.com/th/id/R.8764d162d1e9be6b2cf6d348e2da99f0?rik=TLZjyw4Nspa%2b0w&amp;riu=http%3a%2f%2fpic.616pic.com%2fys_bnew_img%2f00%2f62%2f36%2fr9dfcyoyjR.jpg&amp;ehk=SdJuWe8fxNWlX58TeKImDSWatngZpwGh6ann2DJ%2fXN0%3d&amp;risl=&amp;pid=ImgRaw&amp;r=0" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Codeforces 1100-1400"/></a><div class="content"><a class="title" href="/2023/06/02/Codeforces%201100-1400/" title="Codeforces 1100-1400">Codeforces 1100-1400</a><time datetime="2023-06-02T15:08:21.000Z" title="Created 2023-06-02 23:08:21">2023-06-02</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://pic1.zhimg.com/v2-5ea151d43c0ebb13546985d225ac256a_1200x500.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2024 By 1025</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">Hi, welcome to my <a href="https://lwkblog.gitee.io/">blog</a>!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="Toggle between single-column and double-column"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="chat_btn" type="button" title="Chat"><i class="fas fa-sms"></i></button><a id="to_comment" href="#post-comment" title="Scroll To Comments"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="Back To Top"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">Search</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  Loading the Database</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="Search for Posts" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.1
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container').forEach(node => {
            if (node.hasAttribute('display')) {
              btf.wrap(node, 'div', { class: 'mathjax-overflow' })
            } else {
              btf.wrap(node, 'span', { class: 'mathjax-overflow' })
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax/es5/tex-mml-chtml.min.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script><script>(() => {
  const $mermaidWrap = document.querySelectorAll('#article-container .mermaid-wrap')
  if ($mermaidWrap.length) {
    window.runMermaid = () => {
      window.loadMermaid = true
      const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

      Array.from($mermaidWrap).forEach((item, index) => {
        const mermaidSrc = item.firstElementChild
        const mermaidThemeConfig = '%%{init:{ \'theme\':\'' + theme + '\'}}%%\n'
        const mermaidID = 'mermaid-' + index
        const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent
        mermaid.mermaidAPI.render(mermaidID, mermaidDefinition, (svgCode) => {
          mermaidSrc.insertAdjacentHTML('afterend', svgCode)
        })
      })
    }

    const loadMermaid = () => {
      window.loadMermaid ? runMermaid() : getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaid)
    }

    window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
  }
})()</script><script>function loadValine () {
  function initValine () {
    const valine = new Valine(Object.assign({
      el: '#vcomment',
      appId: 'T4ZmL3yRi9k7xz6QEtS0H2jO-gzGzoHsz',
      appKey: 'dxkZy1hOCexMwxvUS4TBskNa',
      avatar: 'monsterid',
      serverURLs: '',
      emojiMaps: "",
      path: window.location.pathname,
      visitor: false
    }, null))
  }

  if (typeof Valine === 'function') initValine() 
  else getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js').then(initValine)
}

if ('Valine' === 'Valine' || !true) {
  if (true) btf.loadComment(document.getElementById('vcomment'),loadValine)
  else setTimeout(loadValine, 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script><script>function loadDisqus () {
  var disqus_config = function () {
    this.page.url = 'https://lwkblog.gitee.io/2023/03/15/Pytorch%E6%A1%86%E6%9E%B6%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E5%9B%BE%E5%83%8F%E5%88%86%E7%B1%BB%E6%A8%A1%E5%9E%8B/'
    this.page.identifier = '/2023/03/15/Pytorch%E6%A1%86%E6%9E%B6%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0%E5%9B%BE%E5%83%8F%E5%88%86%E7%B1%BB%E6%A8%A1%E5%9E%8B/'
    this.page.title = 'Pytorch框架深度学习图像分类模型'
  };

  window.disqusReset = () => {
    DISQUS.reset({
      reload: true,
      config: disqus_config
    })
  }

  if (window.DISQUS) disqusReset()
  else {
    (function() { 
      var d = document, s = d.createElement('script');
      s.src = 'https://.disqus.com/embed.js';
      s.setAttribute('data-timestamp', +new Date());
      (d.head || d.body).appendChild(s);
    })();
  }

  document.getElementById('darkmode').addEventListener('click', () => {
    setTimeout(() => window.disqusReset(), 200)
  })
}

if ('Valine' === 'Disqus' || !true) {
  if (true) btf.loadComment(document.getElementById('disqus_thread'), loadDisqus)
  else loadDisqus()
} else {
  function loadOtherComment () {
    loadDisqus()
  }
}
</script></div><script src="https://cdn.jsdelivr.net/npm/blueimp-md5/js/md5.min.js"></script><script>window.addEventListener('load', () => {
  const changeContent = (content) => {
    if (content === '') return content

    content = content.replace(/<img.*?src="(.*?)"?[^\>]+>/ig, '[image]') // replace image link
    content = content.replace(/<a[^>]+?href=["']?([^"']+)["']?[^>]*>([^<]+)<\/a>/gi, '[link]') // replace url
    content = content.replace(/<pre><code>.*?<\/pre>/gi, '[code]') // replace code
    content = content.replace(/<[^>]+>/g,"") // remove html tag

    if (content.length > 150) {
      content = content.substring(0,150) + '...'
    }
    return content
  }

  const getIcon = (icon, mail) => {
    if (icon) return icon
    let defaultIcon = '?d=monsterid'
    let iconUrl = `https://gravatar.loli.net/avatar/${md5(mail.toLowerCase()) + defaultIcon}`
    return iconUrl
  }

  const generateHtml = array => {
    let result = ''

    if (array.length) {
      for (let i = 0; i < array.length; i++) {
        result += '<div class=\'aside-list-item\'>'

        if (true) {
          const name = 'src'
          result += `<a href='${array[i].url}' class='thumbnail'><img ${name}='${array[i].avatar}' alt='${array[i].nick}'></a>`
        }

        result += `<div class='content'>
        <a class='comment' href='${array[i].url}' title='${array[i].content}'>${array[i].content}</a>
        <div class='name'><span>${array[i].nick} / </span><time datetime="${array[i].date}">${btf.diffDate(array[i].date, true)}</time></div>
        </div></div>`
      }
    } else {
      result += 'No Comment'
    }

    let $dom = document.querySelector('#card-newest-comments .aside-list')
    $dom.innerHTML= result
    window.lazyLoadInstance && window.lazyLoadInstance.update()
    window.pjax && window.pjax.refresh($dom)
  }

  const getComment = () => {
    const serverURL = 'https://T4ZmL3yR.api.lncldglobal.com'

    var settings = {
      "method": "GET",
      "headers": {
        "X-LC-Id": 'T4ZmL3yRi9k7xz6QEtS0H2jO-gzGzoHsz',
        "X-LC-Key": 'dxkZy1hOCexMwxvUS4TBskNa',
        "Content-Type": "application/json"
      },
    }

    fetch(`${serverURL}/1.1/classes/Comment?limit=6&order=-createdAt`,settings)
      .then(response => response.json())
      .then(data => {
        const valineArray = data.results.map(function (e) {
          return {
            'avatar': getIcon(e.QQAvatar, e.mail),
            'content': changeContent(e.comment),
            'nick': e.nick,
            'url': e.url + '#' + e.objectId,
            'date': e.updatedAt,
          }
        })
        saveToLocal.set('valine-newest-comments', JSON.stringify(valineArray), 10/(60*24))
        generateHtml(valineArray)
      }).catch(e => {
        const $dom = document.querySelector('#card-newest-comments .aside-list')
        $dom.innerHTML= "Unable to get the data, please make sure the settings are correct."
      }) 
  }

  const newestCommentInit = () => {
    if (document.querySelector('#card-newest-comments .aside-list')) {
      const data = saveToLocal.get('valine-newest-comments')
      if (data) {
        generateHtml(JSON.parse(data))
      } else {
        getComment()
      }
    }
  }

  newestCommentInit()
  document.addEventListener('pjax:complete', newestCommentInit)
})</script><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-fluttering-ribbon.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = false;
POWERMODE.mobile = true;
document.body.addEventListener('input', POWERMODE);
</script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"]):not([href="/music/"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>